How can I find the index of an object in an array from that object's script?
Hey guys,
I have a situation in which I have a GameManager script that has an array of game objects called nodes, which are placed in various locations in the scene.
On each of these node objects, I have a script called Node. It has an OnMouseDown function in it. What I'm trying to do is when I click on one of these nodes, it sends information about the node's specific identity to the GameManager script.
The problem I'm having is that I can't figure out how to get the index number for a specific node from within that object's Node script. Is there a way to do this?
This is the code that I have:
GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
public GameObject [] nodes;
public int indexNumber;
void Update ()
{
Debug.Log(indexNumber);
}
}
Node.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Node : MonoBehaviour {
public GameManager _gameManager;
void Start ()
{
_gameManager = GetComponent<GameManager>();
}
void OnMouseDown()
{
_gameManager.indexNumber = 0; // "0" is a substitute for what I would put to find the actual index number for this object in the array
}
}
Your answer
Follow this Question
Related Questions
C# array error index is zero but when i try to assign it to variable it suddenly change to 3 0 Answers
Finding specific index of an array 1 Answer
Parameter Name: Index 0 Answers
Array of Arrays 3 Answers
Array cannot read Greek characters 1 Answer