Understanding Error Causes

Hi there, still a new person so bear with me, but I have two errors for which I’m unclear on the reason. Perhaps this community can help me out again.

  1. I’m keeping a count of matches in my game inside a function with an int matchesFound defined as 0 at the beginning. But at the end, I have this:
    if(matchesFound = 0)
    Debug.Log(“Sorry, No Matches Found.”);

The message I get is: error CS0029: Cannot implicitly convert type int' to bool’ I don’t get why it would consider that a bool, though. My workaround is to use if(matchesFound < 1) but understanding the issue here will help in the future.

  1. I have one function that renames instantiated objects into their grid location in format ‘x-y’, such as 0-0 or 2-3. I’ll need to move those objects later, and thus rename them into the updated coordinates, as well as shifting them to the new grid location. However, I’m not yet experienced enough to determine the issue.

(Info: x and y are looping through the grid, cy is a counter for check locations in another loop.)

string oldnm = x + “-” + (cy + y); //Translate coordinates into old tile’s name
string ren = x + “-” + y; //Translate new coordinates into tile’s new name
GameObject.Find(oldnm).transform.position = new Vector3(x, y, 0f);
GameObject.Find(oldnm).name = ren;

But the error I get here is: NullReferenceException: Object reference not set to an instance of an object I suspect there’s something small but important missing. (EDIT: Eventually I’ll put animation to move the tile from the old position to the new one, for now I just want to relocate it based on the current coordinates then update the name to match.)

Thanks in advance for the suggestions and/or solutions!

  1. The equality operator is ‘==’, not ‘=’.
if (matchesFound == 0) {
}
1 Like

Oh right. Dangit, I keep thinking in old BASIC terms that x = y works for both. Thanks! One down, one to go…

Looks like it’s not finding the gameObject oldnm…

1 Like

Yeesh, that seems to be it. Found the root cause, can’t believe I forgot to include an update in the reference array.

Clearly I’m rusty, but it’s coming back to me! Got both parts working now, thanks again to both of you!

1 Like