- Home /
Script not responding to public variable change
So, I have a script which needs access to a variable from an another script. Accessing seems to be functional, however, Changing the variable state does not seem to affect the other script. Under this, is Script 1, initially containing the variable.
public class MMenuElements : MonoBehaviour {
public bool ElementButton = false;
void OnMouseDown()
{
ElementButton = true;
Debug.Log("Button pressed");
}
void Update ()
{
if(ElementButton == true)
{
Debug.Log("eButtton is true");
}
}
}
Quite simple. Next, there's the Script 2, which needs access to Script 1.
public class MMCam : MonoBehaviour {
private MMenuElements eButton;
void Update ()
{
if(eButton.ElementButton == true)
{
gameObject.SetActive(false);
}
}
}
So, is there anyone who might have a clue at what's causing the problem?
Right, right. Script 2 doesn't seem to be affected at all when I change the value of ElementButton within Script 1.
Hi, your code looks ok. $$anonymous$$aybe you could add a few Debug.Log to check if everything is called in the $$anonymous$$$$anonymous$$Cam code. I would also check that the eButton is assigned to the right element: maybe your are testing by clicking on another one than the one referenced in the $$anonymous$$$$anonymous$$Cam object ? To check this kind of thing I log the object's GetInstanceID().
Well, I seem to get an error: "Object reference not set to an instance of an object", originating from $$anonymous$$$$anonymous$$Cam script. I also do not seem to get any response from the if statement in the $$anonymous$$$$anonymous$$Cam script, however, both Debug.Log-s from the $$anonymous$$$$anonymous$$enuElements script seem to work normally. Any ideas? Edit: I also seem to have a warning, stating: "$$anonymous$$$$anonymous$$Cam.eButton is never assigned to, and will always have its default value null'"
Answer by theredace · Sep 28, 2014 at 08:33 PM
So you have a private variable for script 1 in script 2, but I don't see it being assigned anywhere. You need to either make it public and assign it via the inspector, or assign it in your Start() method using something like GameObject.Find() or GameObject.FindGameObjectWithTag().
Your answer
Follow this Question
Related Questions
Getting a variable from another object 1 Answer
Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer
Accessing variable from a method in another script and gameObject 2 Answers
Access a variable from another script in update function 1 Answer
Help With C# Static Boolean! 2 Answers