- Home /
C# Gameobject's Script's ValuesEquals Other Gameobject's script's Values
I have two gameobjects with have the exact same script attached to both of them. I would like one of the gameobjects's script's values equal the other gameobjects's script's values. But I keep getting the error the left hand side must contain a proper indexer. What am I doing wrong?
public class SomeScript1 : MonoBehaviour {
public gameobject someGameObject1;
public gameobject someGameObject2;
void Start(){
//Both Gameobjects contain the script SomeScript2
someGameObject1.GetComponent<SomeScript2>() = someGameObject2.GetComponent<SomeScript2>();
}
}
Answer by getyour411 · Mar 25, 2014 at 11:01 PM
Instead of public gameObject why not
public SomeScript2 someScript2;
(drag/drop gameobjects containing that Components in Inspector)
and then
myVar = someScript2.myVar;
I tried that and I couldn't find the gameobject in someScript2 . When I saved it I got an error saying that someScript2 doesn't contain a definition of someGameObject2. I also tried someScript2.gameobject and someScript2.transform but those variables are read only.
public class SomeScript1 : $$anonymous$$onoBehaviour {
public gameobject someGameObject1;
public SomeScript2 someScript2;
void Start(){
someGameObject1 = someScript2.someGameObject2;
}
}
Your problem statement said you want to assign script variables why do we keep going round with gameobjects?
thisScriptVar = someScript2.thatScriptVar;
Because I want to change the scripts specifically attached to those gameobjects. Lets say in someScript2 there is an integer called someInt.
public class SomeScript2 : $$anonymous$$onoBehaviour {
public int someInt;
}
I then assign someInt a value for each gameobject. For someGameobject1 someInt equals 5 and someGameobject2 equals 10. Ins$$anonymous$$d of typing someGameObject1.GetComponent().someInt = someGameObject2.GetComponent().someInt I've been trying to see if I can do the exact same thing except with the classes.
public class SomeScript1 : $$anonymous$$onoBehaviour {
public gameobject someGameObject1;
public gameobject someGameObject2;
void Start(){
someGameObject1.GetComponent<SomeScript2>().someInt = 5;
someGameObject2.GetComponent<SomeScript2>().someInt = 10;
//Both Gameobjects contain the script SomeScript2
someGameObject1.GetComponent<SomeScript2>() = someGameObject2.GetComponent<SomeScript2>();
}
}
You want to look at Send$$anonymous$$essage to see if that's something you could use. I still don't get why someint = script2.someint is a problem, but I don't want to beat a dead horse.
Sorry I made a mistake with the code. It's fixed now.
public class SomeScript1 : $$anonymous$$onoBehaviour {
public gameobject someGameObject1;
public gameobject someGameObject2;
void Start(){
someGameObject1.GetComponent<SomeScript2>().someint = 5;
someGameObject2.GetComponent<SomeScript2>().someint = 10;
//Both Gameobjects contain the script SomeScript2
someGameObject1.GetComponent<SomeScript2>() = someGameObject2.GetComponent<SomeScript2>();
}
}
Your answer
Follow this Question
Related Questions
C# Check If Gameobject is within Collider 1 Answer
C# Preserving GameObjects' Previous Meshes 1 Answer
C# Plane Detecting a Gameobject 1 Answer
C# Reverting GameObject to Original 1 Answer
C# Rotate More than Two GameObjects 1 Answer