- Home /
Comparing an object with a Serializible Array with same object returns -1?
Not sure how to explain this best...
I'm working on a puzzle for my game in which the player jumps on 5 rocks in any order, and once they do, they get a reward. I've been handling this through and Event object that contains all the variables and functions needed for the puzzle, like keeping track of which rocks have been jumped on and giving the reward once all 5 are jumped on.
I am doing this through an array that contains a variable isJumpedOn, and the game object in question. I feed the game objects in through the editor, so the script can be reused multiple times in one scene instead of having a global handler, and I can set any amount of rocks to be considered jumpable.
On the rocks, when they are jumped on, they call a code in the event handler and pass themselves as a gameobject to the function that they call. in that function, I try to compare the game object they just passed to the array that I have, but it always returns -1, even though the object is in the array. I can't figure out how to fix this? I need to keep track of specific rocks because it'll be used in a similar Collectathon manner later on.
Event Handler Script:
[System.Serializable]
public class RocksArray
{
public GameObject Rock;
public bool jumpedOn = false;
}
public Material JumpedOnMaterial;
public bool mothSpawned;
public RocksArray[] ListOfRocks;
...
public void JumpOnRock (GameObject other)
{
int rockIndex = System.Array.IndexOf(ListOfRocks, other);
print (rockIndex);
//ListOfRocks [rockIndex].jumpedOn = true;
other.GetComponent<MeshRenderer>().material = JumpedOnMaterial;
}
Rock Script:
void OnTriggerEnter () {
print ("hi");
RockJumpScript.JumpOnRock (gameObject);
}
When I jump on a rock, it should compare itself to the game objects in the array and find which one is itself and give me the index, but instead it's returning -1.
Edit: I tried to test more directly if any of the rocks match the indexes of the array with DifferenceEquals
if (GameObject.ReferenceEquals (ListOfRocks [1], other)) {
print ("they match!");
} else {
print ("these objects are different");
}
But it always returns "these objects are different" even when I jump on the rock that's in index 1. So for some reason, unity doesn't recognize the object in the array with the object in the scene, even though the array is referencing the object in the scene.
Edit 2: if i replace ListOfRocks[1] with ListOfRocks[1].Rock it works. I think the best solution is a for loop...
I figured it out. i declared rockIndex outside of the function and used a for loop and compared things manually.
public void JumpOnRock (GameObject other)
{
for (int i = 0; i < ListOfRocks.Length; i++) {
if (ListOfRocks [i].Rock == other) {
rockIndex = i;
print (rockIndex);
JumpRockDone ();
break;
} else {
rockIndex = -1;
}
}
if (rockIndex != -1 && !ListOfRocks [rockIndex].jumpedOn) {
ListOfRocks [rockIndex].jumpedOn = true;
other.GetComponent<$$anonymous$$eshRenderer>().material = JumpedOn$$anonymous$$aterial;
} else {
Debug.LogWarning ("Rock does not exist in the array!");
}
}
even though i figured it out myself, I hope my answer can help someone else with the same problem.
FYI, you wrote wrong code. int rockIndex = System.Array.IndexOf(ListOfRocks, other); means you are comparing (RocksArray) to (GameObject) .
They cant be same since, RocksArray (a custom class) is not GameObject.
Your answer
![](https://koobas.hobune.stream/wayback/20220612122938im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to find x item in list? 2 Answers
How do I make an array of buttons open corresponding images on click? 1 Answer
Convert BuiltIn array to an Generic List? 1 Answer
snake problem 0 Answers