How do I access an object script variable that is in a 2D array?
Hi. I have a 2D array that adds a class I created that has a gameobject and various details about that GO in it.
What I want to do is attach a script to each GO in the array and be able to access that script and set a bool in there. However, I'm having problems doing that.
This is the class I created for the 'tiles' (it's a Match 3 game I'm making). Each tile has the GO and other info as you can see. I can access the GO tranform etc OK, so I can move them about, but not the script I have on them.
public class Tile
{
public GameObject tileObj;
public string tag;
public bool isHMatched;
public bool isVMatched;
public bool isMovingDown;
public Tile(GameObject obj, string n, bool hMatched, bool vMatched, bool movingDown)
{
isHMatched = hMatched;
isVMatched = vMatched;
isMovingDown = movingDown;
tileObj = obj;
tag = n;
}
}
I iterate through the array with for loops and create the tiles like this
int blockToCreate = Random.Range(0, candyBlock.Length); // Pick a random block to spawn.
GameObject blockCandy = Instantiate(candyBlock[blockToCreate], new Vector3(boardPosition.transform.position.x + blockSize * c, boardPosition.transform.position.y + blockSize * r, boardPosition.transform.position.z), Quaternion.identity); //Spawn the block
blockCandy.transform.parent = boardPosition.transform; // Set the parent of the block to the boardposition gameobject.
tileArray[c, r] = new Tile(blockCandy, blockCandy.tag, false, false,false); // Add the created block to the array.
I then used this code to try and access the variable in the GO script.
tileArray[c, r].tileObj.gameObject.GetComponent<Tile>().isMovingDown = true;
As you can see, this allows me access to the isMovingDown bool in the script on the object, just fine. But when I run the code I get this error.
ArgumentException: GetComponent requires that the requested component 'Tile' derives from MonoBehaviour or Component or is an interface.
So I thought, OK I'll derive from Monobehaviour in the class I created by adding : Monobehaviour to the class.
public class Tile : Monobehavior
{
...
Now when I run, I get the error
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
I've asked around and was told that I should use AddComponant<> to add the script so it can be used. However, I can't find how to use this properly. I've tried this.
tileScript = tileArray[c, r].tileObj.gameObject.AddComponent<BlockScript>();
Then using tilescript. and trying to get access to the variable on the object script but it doesn't work. The autocomplete gives me no option to do that. I suspect (I know) I'm doing this all wrong, so can anyone help me out with this?
I know this was a long post, but I try to give as much information as I can when asking for help. No point skimming over things.
Thanks very much for any help.