How to make each object have an individual Vector3 and Reference that in another script

Hello,
I am making a RTS game. So far I have a “navigate” script that points the AI in a direction and moves towards a public Vector3 called “target” which is in another script called “movingTroops.” Also, in the “movingTroops” script I have a list of gameObjects that are filled with all the selected units. I need a way for each unit to have its own Vector3 which my “movingTroops” can change and my “navigate” script can access and read from. So far I am getting this error:
NullReferenceException: Object reference not set to an instance of an object
navagate.FixedUpdate () (at Assets/scripts/navagate.cs:11)

here is my code in “navigate”:

 movingTroops movingTroops;
   
    void FixedUpdate()
    {
        foreach (var unit in movingTroops.selectedUnits)
        {
          // I am pretty sure the problem is how I am referencing it here.  
             movingTroops = unit.GetComponent<movingTroops>(); 
        }
        if (movingTroops.target != null)
        {
            if (Vector3.Distance(transform.position, movingTroops.target) > .5 && movingTroops.selectedUnits.Contains(gameObject))
            {
                lookAt();
                checkForobj();
                keepToGround();
                moveTowards();
            }
        }
    }

then in my moveTroops code:

public Vector3 target;
    public  static List<GameObject> selectedUnits = new List<GameObject>();
    public static bool isTarget = false;
   
    void Update () {
     
        if (Input.GetMouseButtonDown(1))
        {
            RaycastHit position;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out position))
            {
                target = new Vector3();
                target = position.point;
                Debug.Log(target);
            }
               
        }
    }

To get real help you should paste the whole code. Otherwise we can just guess what the problem is.

The compiler reports an error in line 11 but there is just the opening bracket of the if clause. I’m thinking this is because you didn’t paste the whole code.

How do you initialize the movingTroops variable? And especially the selectedUnits member? Maybe the unit variable in line 8 isn’t initialized.

hey sorry for taking so long to reply here is the rest of the code:

using UnityEngine;
using System.Collections;

public class navagate : MonoBehaviour {
    movingTroops movingTroops;

    void FixedUpdate()
    {
        foreach (var unit in movingTroops.selectedUnits)
        {
             movingTroops = unit.GetComponent<movingTroops>();
        }
        if (movingTroops.target != null)
        {
            if (Vector3.Distance(transform.position, movingTroops.target) > .5 && movingTroops.selectedUnits.Contains(gameObject))
            {
                lookAt();
                checkForobj();
                keepToGround();
                moveTowards();

                // keepFormation();

            }
        }

    }

moving troops code:

using UnityEngine;
using System.Collections.Generic;

public class movingTroops : MonoBehaviour {
 
    //public navagate nav;
    public Vector3 target;
    public  static List<GameObject> selectedUnits = new List<GameObject>();
    public static bool isTarget = false;
 
    void Update () {
   
        if (Input.GetMouseButtonDown(1))
        {
            RaycastHit position;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out position))
            {
                target = new Vector3();
                target = position.point;
                Debug.Log(target);
            }
             
        }
    }
}

the error is now on line 15 with the full code in the navigate script.

hello?

You haven’t done any null-checking in your code.

It’s good practice to always check the value provided by any GetComponent or Find methods just to make sure that the component or gameobject you are looking for actually exists in the scene or gameobject.

 foreach (var unit in movingTroops.selectedUnits)
        {
             movingTroops = unit.GetComponent<movingTroops>();
        }

Firstly since you are using C# you are better off without dynamic typing and foreach. Static typing and for loop is better in 99.99% chases. Besides you are iterating trough list of objects in a parent object while assigning the parent object a new value on each iteration!

As for the issue at hand. your logic is backwards. Your MovingTroops script should be the one sending commands to your units. Also use GetButton instead of GetMouseButton the default name Mouse1 is Fire1 or 2 (customizeable from project settings>Input )

if (Input.GetButton("Fire1"))
        {
            RaycastHit position;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out position))
            {
                target = new Vector3();

                for (int i = 0; i < selectedUnits.count; i++)
                {
                    if (selectedUnits[i] != null)
                    {
                        selectedUnits[i].GoToPoint(target); //method each unit should implement
                    }
                }
            }

        }