- Home /
Checking what slot in a built-in Array my gameobject is in
Hi
I have made a built in array which consists of materials. I have set it up so my gameobject is a different material each time the game is played.
What I need to check though is what slot in the array my gameobject is currently in and if its possible change the tag of the object when its in a certain slot. For example this code below brings up an obvious error but it explains in simple terms what I want to do...
if ( gameObject.renderer.material = colourChanges[0] && collider.gameObject.CompareTag("Red Player Weap"))
{
System.Random random = new System.Random();
gameObject.renderer.material = colourChanges[random.Next(0, colourChanges.Length)];
print ("I have been hit by the correct colour " + collider.gameObject.tag);
}
I am trying to say that if the enemy is currently in slot [0] of my array and its hit by something with correct tag - do something. However the error here is that I cannot...
"Cannot implicitly convert type bool' to
UnityEngine.Material'"
Which makes sense but I have no idea how to solve my issue. All I want to do is say when the enemy is this material/in this slot in the array then do this.
Any help is much obliged as this has been wrapping my brain for hours now. Seriously people any help is appreciated thank you :)
What is in the colourChanges array? Is it a material? It's more natural to compare the materials by their names. And you are assigning, not comparing the materials in your if statement. So something like this should be more convenient: 'gameObject.renderer.material.name == colourChanges[0].name' .
Answer by oneslyfox · Nov 29, 2013 at 12:04 AM
if ( gameObject.renderer.material = colourChanges[0] && collider.gameObject.CompareTag("Red Player Weap"))
Should be:
if ( gameObject.renderer.material == colourChanges[0] && collider.gameObject.CompareTag("Red Player Weap"))
You have '=' instead of '=='. Is that the problem? Your code seems fine otherwise as far as I can tell.
Hi Thank you both for the replies. I have tried using == and it compiles fine but its not doing what I intend it to do.
The material in colourChanges[0] is red and if its hit by a red player weap - it should change material and print...
print ("I have been hit by the correct colour " + collider.gameObject.tag);
but with == it's not working...
if ( gameObject.renderer.material == colourChanges[0] && collider.gameObject.CompareTag("Red Player Weap"))
{
System.Random random = new System.Random();
gameObject.renderer.material = colourChanges[random.Next(0, colourChanges.Length)];
print ("I have been hit by the correct colour " + collider.gameObject.tag);
}
else
{
print ("Wrong colour");
}
Thats my new code
What is in your array colourChanges? Strings or actual materials?
O$$anonymous$$ so I tried this code...
void OnTriggerEnter(Collider collider)
{
if ( gameObject.renderer.material == colourChanges[0])
{
System.Random random = new System.Random();
gameObject.renderer.material = colourChanges[random.Next(0, colourChanges.Length)];
print ("I have been hit by the correct colour " + collider.gameObject.tag);
}
else
{
print ("Wrong colour");
}
and no improvement. The code is not working. It seems that my gameobject is not in the array element 0 - even though in game it is the red material assigned to element [0] in my array.
Any ideas please :)
$$anonymous$$aterials. I assign them in the inspector ![alt text][1]
Just ignore the Red, Blue, Green and Yellow material slots above the actual array I was just testing something else [1]: /storage/temp/18696-untitled-1.jpg
It should be:
if ( collider.gameObject.renderer.material == colourChanges[0])