For-loop problem

Hey there :slight_smile:

The below codes gives a weird error in line 38 which is this line in the below:

positions[i] = new Vector3(Input.mousePosition.x,Input.mousePosition.y,Input.mousePosition.z);

it gives the error:

NullReferenceException: Object reference not set to an instance of an object
  at TrackPosition.Update () [0x0002c] in I:\Minimap\Assets\TrackPosition.cs:38

And I have no idea what to do. linePrefab IS set in unity, and the arrays do have a defined length. After abit of testing, I came to the conclusion that my for-loop is bugged. Maybe some of you can tell me otherwise. :slight_smile:

using UnityEngine;
	// Update is called once per frame.
	void Update () {

        if (!tracking)
        {
            if (Input.GetMouseButtonDown(0))
            {
                for (int i = 0; i < 10; i++)
                {
                    Debug.Log(Input.mousePosition);
                    
                    positions[i] = new Vector3(Input.mousePosition.x,Input.mousePosition.y,Input.mousePosition.z);
                    positions[i].z = 40;
                    worldPos[i] = new Vector3(
                        Camera.main.ScreenToWorldPoint(positions[i]).x,
                        Camera.main.ScreenToWorldPoint(positions[i]).y,
                        40);

                    Instantiate(
                        linePrefab,
                        worldPos[i],
                        Quaternion.Euler(
                            0,
                            0,
                            Divide(
                                Substract(worldPos[i - 1].x, worldPos[i].x),
                                Substract(worldPos[i - 1].y, worldPos[i].y))));
                }
            }
        }
	}

Please help me! ):

EDIT: Substract and Divide are functions that does exactly what the name suggests.

~Mikkelet

no idea what that compile error is all about, but isn’t Input.mousePosition a Vector3 as is? can’t you just say:

positions = Input.mousePosition;

Could be that the positions array is null.

he means - has positions been initialized when it gets to this bit of code?

in Awake or Start you need to do something like “positions = new Vector3[10]”

Yeah, I did that already:

void Start () {
screenPos = new Vector3[trackingSteps];
worldPos = new Vector3[trackingSteps];
 positions = new Vector3[trackingSteps];
	}

trackingSteps is and int and is set to 10.

What about this “worldPos[i - 1]”, if i == 0 then the result will be -1. worldPos[ -1 ] doesn’t exist, anyway that should throw an “Array index out of range” error.

I’ve tested the rest of the code and works fine…

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
	
	private Vector3[] positions;
	private Vector3[] worldPos;
	
	void Start () {
		positions = new Vector3[ 10 ];
		worldPos = new Vector3[ 10 ];
	}
	
	// Update is called once per frame
	void Update () {
		if( Input.GetMouseButtonDown(0) ){
			for( int i = 0; i < 10; i++){
				positions[i] = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 40);
								
                 worldPos[i] = new Vector3(
					Camera.main.ScreenToWorldPoint(positions[i]).x,
					Camera.main.ScreenToWorldPoint(positions[i]).y,
					40
				);

				print("i: " + i + " / pos: " + positions[i] + " / world: " + worldPos[i] );
			}	
		}
	}
}

Thanks! It worked! :smile: