0

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;
2
  • You might look into using some sort of general GameController object to hold your general game information. To get more detailed help, though, you'll probably want to try asking this question on Unity Answers :) Commented Mar 31, 2014 at 20:30
  • 1
    gameObject.GetComponent. Can't see the Unity script reference at work, just google for it. Commented Mar 31, 2014 at 20:34

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.