- Home /
Array elements change Gameobject.tag
Hi
I have an array of materials which I can assign in the inspector. I was wondering is there a way to have each element of the built in array change the tag of the gameobject its attached to.
This will not compile but its just an example of what I want to achieve.
colourChanges [1] = gameObject.tag == "blueEnemy";
I just want each element in the array to change the tag of the gameobject. Is there a way to do this? Any help is greatly appreciated. Thank you for helping me with my uni studies.
Answer by paulygons · Nov 30, 2013 at 04:25 AM
An array itself cannot make a change to a GameObject. An array is just a list of objects. The materials in your array are not associated with a gameObject directly, they are applied to a gameObject's meshRenderer component, and I don't know of a way to find all the gameObjects that use a particular material. In other words, you need an array of gameObjects, or at least an array of components that can then be used to trace back to the component's gameObject, where you can finally use gameObject.tag = "someTag".
Alright thank you for the explanation. An array of gameobjects could be the sure. I could make an array of different meshes of my enemy with different materials on it. I would just need a way to switch models mid-game and change this line of code to work for randomizing models ins$$anonymous$$d of materials...
System.Random random = new System.Random();
gameObject.renderer.material = colourChanges[random.Next(0, colourChanges.Length)];
Your answer
Follow this Question
Related Questions
Comparing tags stored in array with the tag of GameObject (Raycasting) 1 Answer
My array does not update when object is destroyed. How do I fix it? (java) 2 Answers
How can I Raycast to multiple objects 1 Answer
Detect a gameObject's tag via OnCollisionEnter() 5 Answers
Nothing Happens at OnTriggerEnter ? 3 Answers