How to carry over variable (transform.position) and use it as Vector3 for instanitating a prefab

Hi!

**Been stuck for a while figuring out how to fix this problem. Hoping for any help at all!**

public class PlaceNewBaseButton : MonoBehaviour
{
    public GameObject baseMenu;
    public GameObject GeoscapeUi;
    public Button buildNewBase;
    public Button cancelBuild;




    public bool HasBaseBeenBuilt = false;
    public bool BaseSuccessfullyBuilt = false;

    public GameObject baseBox;

    public spawnFighter spawner;

    public Vector3 baseBoxPos;

    public float baseCountCheck = 0;
    public void Start()
    {
    }

    public void Update()
    {
        

        if (baseCountCheck == 0)
        {
            buildBase();
        }

        if(baseCountCheck == 0 && HasBaseBeenBuilt == true)
        {
            CancelInvoke("buildBase");
            Debug.Log("double call bug, look for the script attached to other gameobjects");
        }

        void buildBase()
        {

          

            if (baseCountCheck == 8)
            {
                Debug.Log("You have exceeded the base limit");
                cancelledBuild();
            }




            Time.timeScale = 0f;
            Debug.Log("Waiting for base to be built");
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                   GameObject Base = (GameObject)Instantiate(baseBox, hit.point, Quaternion.identity);
                    Base.transform.position = hit.point;
                    baseBoxPos = Base.transform.position;


                    print(Base.transform.position);
                    baseCountCheck += 1;
                    baseBuilt();


                }

            }
        }
        void baseBuilt()
        {

            HasBaseBeenBuilt = true;
            Time.timeScale = 1f;
            Debug.Log("Base built");
            GeoscapeUi.SetActive(true);
            
            baseMenu.SetActive(false);
        }

        void cancelledBuild()
        {
            Time.timeScale = 1f;
            Debug.Log("Canceled");
            HasBaseBeenBuilt = false;
            baseMenu.SetActive(true);

        }

        cancelBuild.onClick.AddListener(cancelledBuild);
        buildNewBase.onClick.AddListener(buildBase);


    }

}

The variable in question is the baseBoxPos, In my spawning script, i use this to get the position to spawn, but whenever a base is placed, the value in the spawning script goes null, so the spawn will always happen at 0,0,0.

public class spawnFighter : MonoBehaviour
{

    public GameObject interceptor;
    public GameObject baseBox;
    public PlaceNewBaseButton baseInfo;


    private float interceptorspawnCounter = 0f;
    private float maxinterceptorspawnLimit = 2f;

    public Vector3 posOfBase;

    // Start is called before the first frame update
   public void Start()
    {
        posOfBase = GetComponent<PlaceNewBaseButton>().baseBoxPos;

        if (posOfBase == null)
        {
            Debug.Log(posOfBase);
        }


    }

   



    public void SpawnObject()
    {
        if(baseBox.activeSelf == false)
        {
            CancelInvoke("SpawnObject");
        }

        if(interceptorspawnCounter >= maxinterceptorspawnLimit)
        {
            interceptorspawnCounter = 0f;
            maxinterceptorspawnLimit = 2f;
        }

        if(interceptorspawnCounter <= maxinterceptorspawnLimit)
        {
            Debug.Log(posOfBase);
           Instantiate(interceptor,posOfBase,Quaternion.identity);
            
            

            interceptorspawnCounter++;
            

        }
        else
        {
            Debug.Log("You can only send two interceptors at a time");
        }







    }
}

I am completely stuck on this, I don’t know how to move forward, any help at all would be greatly appreciated!

It looks like in your second script you are grabbing the position in your Start method, but in your first script you aren’t actually setting that position until some later time. So when your second script grabs the value its equal to 0, 0, 0, and when that value is updated later your second script does not see that update. Instead you should make sure you grab the base box pos when you actually need it. Try something like this.

public PlaceNewBaseButton baseButton;

// Start is called before the first frame update
public void Start () {
    baseButton = GetComponent<PlaceNewBaseButton> ();

}

public void SpawnObject () {
    //blah blah blah
    Vector3 posOfBase = baseButton.baseBoxPos;
    if (interceptorspawnCounter <= maxinterceptorspawnLimit) {
        Debug.Log (posOfBase);
        Instantiate (interceptor, posOfBase, Quaternion.identity);
    }
}

The issue is that Vector3 is a struct, not a class, so when you say something like posOfBase = GetComponent<PlaceNewBaseButton>().baseBoxPos; you are making a copy of the value of baseBoxPos but you are not getting a reference to the data itself. This means that if the original data changes its value, your copy of the data will not.

shouldn’t the code be more like baseButton.transform.position ?

edit: at this line Vector3 posOfBase = baseButton.baseBoxPos;