Title says it all but for some reason my script ends up setting off “NullReferenceException: Object reference not set to an instance of an object” when it really shouldn’t be.
I’ve checked to see that all the objects have Tranforms, which they do! I used Debug.Log() to make sure the math works out, which does. All the parts seem to be fine but when the code goes to put the Vector3 into the array, the exception is called.
public class SelectMove : MonoBehaviour {
private static Vector3[] worldposition;
private static GameObject[] worlds;
private void Start()
{
worlds = GameObject.FindGameObjectsWithTag("Worldddd");
for (int i = 0; i < 2; i++)
{
worldposition _= (transform.position - worlds*.GetComponent<Transform>().position); //this sets off the Exception*_
first of all, the variable worldposition is just defined, but as I see here, it’s never initialized. You need to initialize it before you can use it. And also, you seem to check for an exact amount of objects in your for loop: (int i = 0; i < 2; i++) and GameObject.FindGameObjectsWithTag("Worldddd"); may not return that many objects. For example it may return only two, so that way the third one (worlds[2]) may not exist and you will bump again into an error.
Just try:
public class SelectMove : MonoBehaviour {
private static Vector3[] worldposition;
private static GameObject[] worlds;
private void Start()
{
worlds = GameObject.FindGameObjectsWithTag("Worldddd");
worldposition = worlds.Length;
for (int i = 0; i < worlds .length; i++)
{
worldposition _= (transform.position - worlds*.transform.position);*_