- Home /
Question by
Monsterwald · Jun 18, 2021 at 06:11 PM ·
arrayinteger
Compare array index with integer?
Okay I ran into a wall and after 6 hours of trying and failing, my head literally burns, I have multiple rings, I like to fly through them in a specific order. It's free flight movement. Issue: I have to fly through the very first one in my array (which are Gamobjects), how do I even detect the first one? I cannot do something like that:
for (int i = 0; i < rings.Length; i++)
{
if (rings[i] == ringNumber) ringnumber++
}
rings[ringNumber].GetComponent<MeshRenderer>().material = activeMat;
i have to, somehow transform an array gameobject into an integer.... I don't know.
Player object: private void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag("Ring"))
{
other.transform.parent.GetComponent<Rings>().ringNumber++;
}
}
only code I have... with this method, the order does not matter at all. I can fly through the same ring over and over and it still counts...
Comment
Best Answer
Answer by Hellium · Jun 18, 2021 at 06:27 PM
// Collision script
other.transform.parent.GetComponent<Rings>().RingCrossed(other);
// Rings script
public GameObject[] rings;
private int nextRingToCross;
public void RingCrossed(GameObject crossedRing)
{
if(nextRingToCross == rings.Length)
{
Debug.Log("You've already crossed all the rings");
return;
}
if(crossedRing == rings[nextRingToCross])
{
Debug.Log("You've crossed the correct ring!");
crossedRing.GetComponent<MeshRenderer>().material = activeMat;
nextRingToCross++;
}
else
{
Debug.LogWarning("You haven't crossed the correct ring!");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613035207im_/https://answers.unity.com/themes/thub/images/avi.jpg)