- Home /
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
Are you expecting this code to run every five seconds, or just once five seconds after it starts?
Also, what is "Needles?" Do you mean to be calling Dashboard.Run (Needles.richtung);
?
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
still not working, maaan i could $$anonymous$$r out my hair right now
Is the filename Dashboard.js
? If so, Start ()
won't be executed because you've declared Dashboard as a non-$$anonymous$$onobehaviour-derived class.
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;
}
}
}
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
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?
Answer by Aldwoni_legacy · Jul 19, 2011 at 02:04 PM
tried change Start into Update?
tried already, same weird result
anyway thank you for answering
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?
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...
if you add a Debug.Log(" started"); in the start function. do you see that?
if i put a print statement in Start() then i can see it.
Your answer
Follow this Question
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