Pregunta

I made a game have two objects in the hierarchy panel. One called GameScreen and another called Clock I set up. Each has its own script attached. GameScreen uses game.js and the other uses clock.js.

When I win the game a popup box appears and says, "You've won!" This is caused by the game.js script. However, the clock.js is still running, so my clock is still counting down. When the time is up the clock.js makes a popup box saying, "you lose!"

This causes for a "you win" screen to pop up when you win and then later a you lose screen to appear. As you can probably guess, this is super annoying. If there was a way I could change variables in one script from another,, I could get the clock to stop when you won or I could get the game to stop when the time ran out.

Is there some way to do this??

For example here are two javascript files one on clock and the other on GameScreen . I want the first one to change the variable changeMe in the second to two. 1.js:

function start(){


}

2.js:

var changeMe:int;
¿Fue útil?

Solución

When you win the game, you can change clock's variable from game.js this way:

GameObject.Find("Clock").GetComponent("clock.js").variable_name = new_value;

, where [variable_name] and [new_value] obviously depend on your project. variable_name has to be an exposed variable.

Otros consejos

You can simply use SendMessage() it will make you able to call a function from another object easily.Basically you need to write a function to destroy the clock after wining the game,let's say your function is called destroyClock so you should add that to your game.js script:

gameobject.Find("here goes the name of your clock obj").sendMessage("destroyClock")

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top