Hello I am working on a game right now that has a timer count down until it reaches zero. The code that controls the timer is in one script and i have another javascript file that makes the rest of the game run. In this file it is able to detect if the task was completed and the player won.
I want the timer to stop if the player won. Is there some form of inheritance with javascript so that i can use the playerHasWon variable to stop the clock?
i could just reuse the code in the clockscript but i would prefer to do it through inheritance. If inheritance is possible with javascript. I am new to the language but understand programming constructs.
Edit: i guess all i really need to the playerHasWon variable to be accessible through all the javascript files like a global variable
UnityScript supports inheritance, but it doesn’t sound like that’s what you need here. Given what you describe, I think the most straightforward solution might be to link the components together using references so that they can interact directly.
For info on linking game objects and components in this way, see here.
i dont think i was being clear with my problem so i will try again.
I have two differnt javascript files
controls the game and decides whether or not the player has won
controls the countdown timer that shows how much longer the player has left
i have the variable playerHasWon in file number 1 and i need access to that variable in file number 2 so that i can stop the timer once player has won.
so how would i access the playerHasWon variable in file 2?
That was how I understood the problem initially, and the information on accessing other game objects is relevant, I think.
Any game object can access any other game object via a variety of means, some (or perhaps all) of which are detailed in the link I provided. Once you have access to a game object, you can then acquire a reference to any component of that object using GetComponent(). In this way, for all practical purposes any component in the scene can interact directly with any other component in the scene. In your case, for example, the ‘game controller’ component and the ‘countdown timer’ component can interact with one another so as to stop the timer when the level is complete.
As for accessing specific fields of a script/class, you can make those fields public, or you can provide accessors or public functions that implement specific behaviors.
To summarize, there are three steps involved here, more or less:
Get a reference to the game object of interest.
Get a reference to the component of interest.
Interact with the component via its public interface.
There are ways to bypass step 1 as well (for example, you can assign a reference to the component directly, or search for the component by type, although that may be less efficient than other options, depending).
If that still doesn’t answer the question, perhaps you could clarify which of the steps described above you’re unsure of.
Ok i am getting closer to understanding how to do this, keep in mind i am new to javascript so i might ask some pretty stupid questions.
the code above is from Unitys example of accessing other game objects. What I think i am understanding is that after the code is executed otherScript is now a reference to OtherScript.js and otherScript.DoSomething() is calling the DoSomething function of the OtherScript.js file
and since i need access to the variable playerHasWon i would replace otherScript.DoSomething() with
Yes, that’s accurate. (Technically otherScript is a reference to an instance of the class defined in OtherScript.js, but yes, you’ve got the right idea.)
ok well i think might have a problem then, since in the OtherScript.js it has an instance of a Card class. The card class has two instance variables and playerHasWon is not one of them, it is just a global variable accessible by all the functions on that javascript file. Is my way around that to just add the variable playerHasWon to be an instance variable in the Card class so that i can access it?
I’m not sure I follow completely; you might just go ahead and post your OtherScript.js file so we can take a look at it.
(It sounds to me though like ‘playerHasWon’ is just a member variable, in which case you should be able to access it as ‘otherScript.playerHasWon’, provided the variable is public.)
For what it’s worth, C# is more similar to Java than is UnityScript, so it might be that you’d find C# to be more immediately intuitive.
However, I think the problem here is language-independent, more or less, and would be the same regardless of what language you were using.
The problem is likely with this line:
otherScript = GetComponent(gameScript);
Without additional qualification, GetComponent() searches for the specified component in the same game object to which the script containing the GetComponent() call is attached. If the game object to which the clockScript component is attached doesn’t have a gameScript component attached, then you’ll get a null reference exception when you try to deference ‘otherScript’.
If the scripts are in fact attached to different game objects, then you’ll need to get the two objects communicating in one way or another; this is where the link on ‘accessing other game objects’ that I posted earlier comes into play.
Alright thanks it worked like a champ! all i did was link the gameScript.js to the clock object then change one small piece to the code and i am good to go.
however, if i were using java wouldnt all i have to do is extend the clockScript class from gameScript and the i would get the gameScripts variables through mutator methods.
The reason i am using javascript currently is because i bought a book on Unity and the language it uses is javascript, but once i am done with the book i will try and switch over to c#
Although the details differ somewhat, inheritance is supported in all of the languages in question (Java, UnityScript/JavaScript, C#, and presumably Boo as well, although I haven’t used Boo myself).
So, you could do what you describe in UnityScript. However, the cases where you’d want to use inheritance when working in Unity are fairly rare, I think (although not nonexistent). Usually, you’d just use multiple components/scripts instead. And even outside of Unity, it’s usually recommended to favor composition over inheritance, and only to use inheritance where there’s a clear ‘is a’ relationship.
ok, well there still is a lot i have to learn then haha, i just took java in ap computer science as a jr in HS so that is the language i am most familiar with now, and just hope to learn more and more languages