Error CS0023: Operator '!' cannot be applied to operand of type 'UnityEngine.GameObject[]'
Error CS0023: Operator '!' cannot be applied to operand of type 'UnityEngine.GameObject[]'
Hello guys, I'm learning how to use unity and I came up with a annoying error that I can't seem to find an answer. This is the part of the code that has an error and the objective is if it can't find an object with that tag then spawns it however it gives the error above. I would really appreciate if someone could give me a hand.
Transform musicPrefab;
void Start ()
{
currentScore = 0;
if (!GameObject.FindGameObjectsWithTag ("MM"))
{
var mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity); //Spawns the music
}
}
Answer by acorrow · Dec 13, 2015 at 05:35 PM
This isn't a Unity issue, its a C# issue.
You are using GameObject.FindGameObjectsWithTag... PLURAL... objectS.... This returns an array of GameObject (even if that array is 0 or 1 length), and you can't just use the not equal (!) operator against a collection.
If you really are looking for a collection to be equal, then you need to iterate over the collection, use linq, write a custom class/comparer, etc.... OR if you are actually just looking for a singleton GameObject, use GameObject.FindGameObjectWIthTag (no S)..
Cheers!
Your answer
Follow this Question
Related Questions
C# Greater Than or Equal To 1 Answer
Can't Locate gameobject in array 3 Answers
problem with lists 0 Answers
Infinite Time inbetween if statements? 2 Answers
if statements are running when they are not suppose to be 1 Answer