- Home /
Having a script variable update between two objects
Hi everyone,
I've got a javascript for being able to go to the next level. You are meant to be able to pick up a key and then go to a trapdoor, and this will take you to the next level. However, this doesn't work.
Code follows
var isKey:boolean = false;
public var hasKey:boolean = false;
var key:GameObject;
function Start () {
}
function Update () {
if(hasKey == true)
{
hasKey = true;
}
}
function OnTriggerEnter(collider)
{
if(hasKey == false && isKey == true);
{
hasKey = true;
key.SetActive(false);
}
if(hasKey == true && isKey == false)
{
Application.LoadLevel(5);
}
}
This script is applied to both the cube with collider acting as the trapdoor and the key. Unfortunately, it doesn't take you to the next level.
Does anyone have any ideas?
Answer by Jeff-Kesselman · Nov 17, 2014 at 03:48 AM
I have an idea...the idea is that we need to teach you basic debugging.
Start by putting a Debug.Log(...) at key points in your code to see what is and isnt working, like this...
function OnTriggerEnter(collider)
{
Debug.Log("In OnTriggerEnter");
if(hasKey == false && isKey == true);
{
Debug.Log("in hasKey and is Key");
hasKey = true;
key.SetActive(false);
}
if(hasKey == true && isKey == false)
{
Debug.Log("in hasKey and not is Key");
Application.LoadLevel(5);
}
}
Also... parenthesize your if clauses to ensure that the order of operations isn't screwing you up. eg
if((hasKey == false) && (isKey == true))
I DO see an error in your code... but Im not going to point it out to you because you will learn more if you find it yourself.
Start with the debug statements and see what is and isn't happening like you expect.... You should never, ever, ever count on order of operations when programming.