- Home /
Running script on multiple objects but script only runs once
I have a GUI list that I want checked off whenever an object is clicked. Right now I can get one object to work but when I want to run it on multiple objects, the script stops working. Thanks in advance
Here's my script:
static var state: int = 0;
var state0tex : Texture2D; var state1tex :Texture2D; var objectName : String;
function Start() { guiTexture.enabled = true; state = 0;
}
function Update () { if (state == 1 && GameObject.Find(objectName) == true){ guiTexture.texture = state1tex; } }
That fixed one thing but created another problem. The other script I use to trigger the TextGUI.js script above now has an error:
"An instance of type 'TextGUI' is required to access non static member 'state'."
Sorry this is my first week learning javascript so bear with me. Thanks in advance.
function OnMouseUp () {
TextGUI.state++;
Destroy(gameObject);
}
This is what I have added so far.. I don't know if it's of any help
var TextGUI : TextGUI;
function Start() { TextGUI = GetComponent.<TextGUI>(); }
function OnMouseUp () { // not sure what to put here now Destroy(gameObject); }
Answer by Eric5h5 · Mar 03, 2011 at 11:50 PM
Don't use static variables. Static = one instance per class/script only.
Answer by Alan 2 · Mar 04, 2011 at 01:45 AM
That fixed one thing but created another problem. The other script I use to trigger the TextGUI.js script above now has an error:
"An instance of type 'TextGUI' is required to access non static member 'state'."
Sorry this is my first week learning javascript so bear with me. Thanks in advance.
function OnMouseUp () {
TextGUI.state++;
Destroy(gameObject);
}
This is what I have added so far.. I don't know if it's of any help
var TextGUI : TextGUI;
function Start() { TextGUI = GetComponent.<TextGUI>(); }
function OnMouseUp () { // not sure what to put here now Destroy(gameObject); }
Please don't post comments as answers. You can edit your question. Look up "accessing other game objects" and "GetComponent" in the docs.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111255im_/https://answers.unity.com/themes/thub/images/avi.jpg)