- Home /
Same script on multiple game objects [solved]
I have a class like this:
public class Myclass{
public int somevar;
public string somevar2;
//constructor
//public set method
//public get method
}
And inside a script that i attach to multiple objects I do something like(the objects have a box collider with "is trigger" checked).
public myScript : MonoBehaviour{
Myclass theObject;
void Start(){
//initialize theObject with some values
}
void OnTriggerEnter()
{
print(theObject.GetSomething());
}
void Update(){
if(name == "Object1")
theObject.SetSomething("value");
}
As you can see,i change some value of theObject only for Object1. But when i check the print log, every object that had that script attached has the same value even if i only changed it for Object1. Isn't Unity supposed to have different instances of objects for different GameObjects with the same script ?
What am I doing wrong, clearly there is something I am missing seeing I am new to Unity?
I figured it out eventually, I am so tired i didn't even notice the problem. I was updating the value inside the Update() function on GUITexture click event, thus it didn't matter what Trigger caused the GUITexture to appear.
Congrads on solving your own problem. Set your question as solved, please :)
Your answer

Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Values lost? 1 Answer
Calling a method of a class that is a part of another class? 1 Answer
Check if gameobject has script with inheritance 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers