- Home /
C'# When Taking Variables From Other Scripts, The Values Do Not Update
Hi, cheers for reading;
-Two classes: Map_Mech & Map_Tile_Generic
-There is one object that contains the script Map_Mech, that object is called pMap_Mech (prefab)
-There are potentially hundreds of clones of the prefab (pTile) that contains the Map_Tile_Generic script. I want the Map_Tile_Generic script to take a value from the single Map_Mech, and use it (accessor functions). I have drag/dropped the Map_Mech prefab into the script area in the pTile prefab, and it finds it (`other.getTileScale()`) sets to the correct value, as its only assigned once, on create)
Map_Mech:
public Vector3 GetNewVector () { return vNewVector; }
"Map_Tile_Generic"
private GameObject oBase;
public Map_Mech other;
void Awake()
{
oBase = GameObject.CreatePrimitive (PrimitiveType.Cube);
oBase.transform.position = other.vNewVector;
oBase.transform.localScale = other.GetTileScale();
}
Map_Mech changes the value of vNewVector in real time, however, when I use the function, it always returns Vector3(0,0,0); I managed to solve my previous question after posting, so I'm hoping I can do the same XD However, if anyone does know (or has an idea), the info would be fantastic! I'm online every day between 10am-10pm GMT (usually). Ta
Answer by aldonaletto · Mar 13, 2012 at 12:18 PM
If I understood correctly, other is a reference to the prefab Map_Mech - but it should be a reference to the actual instance of Map_Mech that exists in your scene. Instead of assigning other in the Editor, you should find it at runtime, and in Start, not Awake (Awake is called during object creation, and other objects may not exist yet):
public Map_Mech other;
void Start()
{
other = GameObject.Find("Map_Mech"); // find Map_Mech instance by name
oBase = GameObject.CreatePrimitive (PrimitiveType.Cube);
oBase.transform.position = other.vNewVector;
oBase.transform.localScale = other.GetTileScale();
}
Thanks for the reply! Questions are numbered:
Put code in, but I get the same error that I got while trying to get it to work previously: "Cannot implicitly convert 'UnityEngine.GameObject' to '$$anonymous$$ap_$$anonymous$$ech'. Changing the private $$anonymous$$ap_$$anonymous$$ech other; to private GameObject other also returns two errors, because now I cannot access GetNewVector() or GetTileScale(). Do I need to use GetComponent? That's what other use, but I keep getting errors about finding specific instances. The only $$anonymous$$ap_$$anonymous$$ech find I can use is FindObjectofType(), which I dont know how to use.
Is the removal of the accessor function intentional in your reply? If so, why so? :)
Once again, cheers!
1- $$anonymous$$y fault! GameObject.Find finds an instance by name and returns its GameObject reference, but other is a $$anonymous$$ap_$$anonymous$$ech variable, which I suppose is a reference to a script $$anonymous$$ap_$$anonymous$$ech.cs. Supposing $$anonymous$$ap_$$anonymous$$ech.cs is attached to the object named "$$anonymous$$ap$$anonymous$$ech", you should do the following:
other = GameObject.Find("$$anonymous$$ap$$anonymous$$ech").GetComponent< $$anonymous$$ap_$$anonymous$$ech>();
oBase = ...
2- I only copy/pasted the relevant lines, thus the oBase declaration was not included in the answer - but you will need it, for sure!
Unfortunately, again, it doesn't work. Same thing as happened at the start. This code compiles fine:
private GameObject oBase; public GameObject other; void Start(){
other = GameObject.FindGameObjectWithTag("p$$anonymous$$ap_$$anonymous$$ech").GetComponent<$$anonymous$$ap_$$anonymous$$ech>(); oBase = GameObject.CreatePrimitive (PrimitiveType.Cube); oBase.transform.localPosition = other.vNewVector; oBase.transform.localScale = other.GetTileScale(); }
However, in debug, "other" returns null. Interestingly, when I use this: "other = GameObject.FindGameObjectWithTag("p$$anonymous$$ap_$$anonymous$$ech")/.GetComponent<$$anonymous$$ap_$$anonymous$$ech>()/;" OR "other = GameObject.FindGameObjectWithTag("p$$anonymous$$ap_$$anonymous$$ech");" it finds the exact gameobject. The get component just does not seem to update.
I am lost.
The correct syntax is GameObject.FindWithTag (don't know if FindGameObjectWithTag works too). Fix it, and try to get the p$$anonymous$$ap_$$anonymous$$ech tagged object first, then get the component $$anonymous$$ap_$$anonymous$$ech:
void Start() { // find $$anonymous$$ap_$$anonymous$$ech instance by tag GameObject map = GameObject.FindWithTag("p$$anonymous$$ap_$$anonymous$$ech"); other = map.GetComponent< $$anonymous$$ap_$$anonymous$$ech>(); // get $$anonymous$$ap_$$anonymous$$ech oBase = GameObject.CreatePrimitive (PrimitiveType.Cube); oBase.transform.position = other.vNewVector; oBase.transform.localScale = other.GetTileScale(); }
Your answer
Follow this Question
Related Questions
How to reference another script and call a function in C# ? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers