Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by biohazard · Jul 19, 2011 at 01:59 PM · javascriptclassesoopswitch-case

Help me with this JavaScript class please(Script inside)

Hey guys, so i'm trying to restructure my mess of a code and handling it via classes, but my first class does like nothing at all!

the value i'm changing does change in the editor but the console won't print!

EDIT : New script yet again!

 var knopfbreite = 75;
 var knopfhoehe = 35;
 class Dashboard extends MonoBehaviour
 {
     public var richtung : int = 0;
     
     
     public function Run(richtung)
     {
         
         print("in funktion");
         yield WaitForSeconds(5.0);
         switch (richtung)
         {
             case 0 : print("idle");
                      break;
             
             case 1 : print("Erster Wert!");
                      break;
             
             case 2 : print("Rewind!");
                      break;
             
             case 3 : print("Play!");
                      break;
             
             case 4 : print("Stop!");
                      break;
             
             case 5 : print("Forward!");
                      break;
             
             case 6 : print("Letzter Wert!");
                      break;
             
             case 7 : Application.Quit();
         }
     }
 };
 
 
 
 
 
 
 function Start()
 {
 print("running");
 Needles.Run(4);
     
 }
 
 
 
 
 
 
 function OnGUI()
 {
     if(GUI.Button(Rect(10,10,knopfbreite,knopfhoehe),"Start"))
     {
         Needles.richtung = 1;
     }
     
     if(GUI.Button(Rect(85,10,knopfbreite,knopfhoehe),"Rewind"))
     {
         Needles.richtung = 2;
     }
     
     if(GUI.Button(Rect(160,10,knopfbreite,knopfhoehe),"Play"))
     {
         Needles.richtung = 3;
     }
     
     if(GUI.Button(Rect(235,10,knopfbreite,knopfhoehe),"Stop"))
     {
         Needles.richtung = 4;
     }
     
     if(GUI.Button(Rect(310,10,knopfbreite,knopfhoehe),"Forward"))
     {
         Needles.richtung = 5;
     }
     
     if(GUI.Button(Rect(385,10,knopfbreite,knopfhoehe),"End"))
     {
         Needles.richtung = 6;
     }
     
     if(GUI.Button(Rect(460,10,knopfbreite,knopfhoehe),"Quit"))
     {
         Needles.richtung = 7;
     }
 }    
 
 function Update() {
 
     
     
 }





Any help is appreciated!

thanks in advance

bio

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image burnumd · Jul 19, 2011 at 02:34 PM 0
Share

Are you expecting this code to run every five seconds, or just once five seconds after it starts?

avatar image burnumd · Jul 19, 2011 at 02:36 PM 0
Share

Also, what is "Needles?" Do you mean to be calling Dashboard.Run (Needles.richtung);?

avatar image biohazard · Jul 19, 2011 at 02:44 PM 0
Share

Needles was meant to be an instance of my class, going to edit my script right away since that's wrong in java script... thanks for the heads-up

avatar image biohazard · Jul 19, 2011 at 02:47 PM 0
Share

still not working, maaan i could $$anonymous$$r out my hair right now

avatar image burnumd · Jul 19, 2011 at 02:48 PM 0
Share

Is the filename Dashboard.js? If so, Start () won't be executed because you've declared Dashboard as a non-$$anonymous$$onobehaviour-derived class.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Evil-Dog · Jul 19, 2011 at 03:04 PM

Once you have modified your class to extend from MonoBehaviour as this

 class Dashboard extends MonoBehaviour 

Your class is now ready to be applied to a game object, Unity works with gameobjects and components, so what you need to do to instantiate your object is to actually create an empty gameobject in your scene and drop your script on it.

It will be an invisible object with an instance of your component. Now with your code, it seems you want to control your dashboard object from outside of it, so either you have one gameobject with a global script and your Dashboard instance as another game object. In that case, your global script can get a reference to your dashboard in 2 ways:

Either you expose a public variable where you'll be able to set your dashboard instance

 public var m_Dashboard:Dashboard;

Or you can get the reference from the scene manually in your Start or Awake function

 function Start()
 {
     GameObject.Find("TheNameOfYourDashboardObjectInTheScene").GetComponent(Dashboard);
 }

From there you can access your m_Dashboard object.

Or, your Dashboard script can do it all, the OnGUI stuff and everything.

I believe a good read for you would be the GameObject part of the Unity documention which explains Unity's philosophy about Game objects and components.

Also some references on how to access objects and components.

And really, more generally, the scripting reference talks about some common unity scripting techniques which could settle some of these unknowns for you

As an attempt to do what you wanna do with only one game object, I suggest this script, now I'm not sure what the OnGUI part does and I assume it listened to button clicks and that it's working this way. Notice a couple things, the richtung variable now initialized to 4 directly and the update function directly running and checking the richtung variable. So you create an empty object and drop this script on it and if OnGUI works as you coded it, it should all work and print Stop! at first until you click some buttons.

 class Dashboard extends MonoBehaviour
 {
     static var knopfbreite = 75;
     static var knopfhoehe = 35;
   
   private var richtung : int = 4;
 
     function Update()
     {
 
         print("in funktion");
         yield WaitForSeconds(5.0);
         switch (richtung)
         {
             case 0 : print("idle");
                      break;
 
             case 1 : print("Erster Wert!");
                      break;
 
             case 2 : print("Rewind!");
                      break;
 
             case 3 : print("Play!");
                      break;
 
             case 4 : print("Stop!");
                      break;
 
             case 5 : print("Forward!");
                      break;
 
             case 6 : print("Letzter Wert!");
                      break;
 
             case 7 : Application.Quit();
         }
     }
     
     function OnGUI()
         {
         if(GUI.Button(Rect(10,10,knopfbreite,knopfhoehe),"Start"))
         {
             richtung = 1;
         }
     
         if(GUI.Button(Rect(85,10,knopfbreite,knopfhoehe),"Rewind"))
         {
             richtung = 2;
         }
     
         if(GUI.Button(Rect(160,10,knopfbreite,knopfhoehe),"Play"))
         {
             richtung = 3;
         }
     
         if(GUI.Button(Rect(235,10,knopfbreite,knopfhoehe),"Stop"))
         {
             richtung = 4;
         }
     
         if(GUI.Button(Rect(310,10,knopfbreite,knopfhoehe),"Forward"))
         {
             richtung = 5;
         }
     
         if(GUI.Button(Rect(385,10,knopfbreite,knopfhoehe),"End"))
         {
             richtung = 6;
         }
     
         if(GUI.Button(Rect(460,10,knopfbreite,knopfhoehe),"Quit"))
         {
             richtung = 7;
         }
         }  
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image biohazard · Jul 19, 2011 at 03:06 PM 0
Share

thanks for the polite answer, my problem is i've been working with C++ if it comes to OOP sofar, the syntax is just way too different.

i'll look these up and do my best to improve

bio

avatar image Evil-Dog · Jul 19, 2011 at 03:19 PM 0
Share

I edited my answer to include a potential script solution. $$anonymous$$aybe the yield doesn't go there anymore, what was your goal with it?

avatar image biohazard · Jul 21, 2011 at 07:28 AM 0
Share

how am i supposed to call these functions?

avatar image
0

Answer by Aldwoni_legacy · Jul 19, 2011 at 02:04 PM

tried change Start into Update?

Comment
Add comment · Show 14 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image biohazard · Jul 19, 2011 at 02:06 PM 0
Share

tried already, same weird result

anyway thank you for answering

avatar image Aldwoni_legacy · Jul 19, 2011 at 02:08 PM 0
Share

what is the result?

you know that a " Start" function is only activated one time and a " Update" function around 30 times a $$anonymous$$ute?

avatar image biohazard · Jul 19, 2011 at 02:10 PM 0
Share

Update, my friend, is called once a frame so i guess "30 times a $$anonymous$$ute" was a typing mistake :) anyways, no printing in the console, not even printing the "in function" statement...

avatar image Aldwoni_legacy · Jul 19, 2011 at 02:19 PM 0
Share

if you add a Debug.Log(" started"); in the start function. do you see that?

avatar image biohazard · Jul 19, 2011 at 02:20 PM 0
Share

if i put a print statement in Start() then i can see it.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with class in Javascript 1 Answer

A node in a childnode? 1 Answer

Assigning variables to GameObjects 2 Answers

'enabled' is not a member of 'Object' 1 Answer

Converting Code Problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges