- Home /
Changing variable of another object (C#)
Can some one help me please. I can't get my head around this problem. I am new to Unity and and still learning C#. So I thought I start with a simple board game.
I have an two objects:
Object 'TilePre' has a script with a public class called 'Goban' and a public string variable called 'status'
I would like to tell Object 'Goban' to change the value of the 'status' variable in class 'A'.
Can some one help? I tried the following links but I can't get it to work.
I set the code back as it was before I tried the above methods.
Goban
using UnityEngine; using System.Collections;
public class Goban : MonoBehaviour {
public GameObject TilePre; public int gobanSize = 6;
// Use this for initialization void Start () { int rows = gobanSize; int columns = gobanSize;
string [,] StatusArray = new string [rows, columns];
for ( int i = 0; i < rows; i++)
{
for ( int j = 0; j < columns; j++)
{
Vector3 position = new Vector3 (i, 0, j);
Instantiate (TilePre, position, Quaternion.identity);
TilePre.name = "Tile_" + i + "_" + j;
TilePre.status = "E";
}
}
}
}
TileScript
using UnityEngine; using System.Collections;
public class TileScript : MonoBehaviour { public GameObject BeadWhitePre; public string tileStatus;
void OnMouseDown () {
Vector3 position = new Vector3 (transform.position.x, (transform.position.y + transform.localScale.y / 2) + (BeadWhitePre.transform.localScale.y / 2), transform.position.z);
Instantiate (BeadWhitePre, position, Quaternion.identity);
TilePre.tileStatus = "W";
}
I get the following ERROR: error CS1061: Type UnityEngine.GameObject' does not contain a definition for
tileStatus' and no extension method tileStatus' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
The code : TilePre.status = "E"; in the Goban class is the problem. How do I fix this?
Answer by Jesse Anders · Oct 23, 2010 at 01:44 PM
I see at least two problems right off. The first is that you're instantiating a new object from the TilePre game object, but then you're modifying the original game object (the one that the new game object was instantiated from). I'm assuming what you're wanting is to modify the newly created clone; to do that, cast the return value of Instantiate() to a GameObject, and that will give you a reference to the new clone.
The second problem is that GameObject does not have a field called tileStatus. When you write TilePre.tileStatus, since TilePre is of type GameObject, the compiler goes looking for a member in the GameObject class called tileStatus. There is no such member, thus the error.
It looks like what you're wanting is to access the tileStatus field of a TileScript component that's attached to the game object. To facilitate this, you can use the GetComponent() function to acquire a reference to the component in question.
Thank you Jesse. That helped. I changed contents in the for loop in the Goban class to: Vector3 position = new Vector3 (i, 0, j); GameObject tileClone = Instantiate(TilePre, position, Quaternion.identity) as GameObject; tileClone.name = "Tile_" + i + "_" + j; tileClone.GetComponent ().tileStatus="E"; That fixed both of the problems you mentioned problem. I learnt a lot today :)