- Home /
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!
You're trying to get the value of baseBoxPos in the Start() method, which runs before the value of baseBoxPos is set in the Update() method. You would need to retrieve it at a later point, such as after Input.Get$$anonymous$$ouseButtonDown(0)
, when it is set in the first script. @DuxFortis
Answer by unity_ek98vnTRplGj8Q · Jan 04, 2021 at 08:38 PM
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.
Hey, I stopped getting the Null exception error on making the baseBox object, and now only get it during the click event to spawn an interceptor! I think it has something to do with the PlaceNewBaseButton script. Thanks for the help so far!
Can you paste your new code and tell me exactly which line is throwing the null ref exception?
Sure! it's line 32 which is the declared local variable posOfBase right at the start of SpawnObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spawnFighter : $$anonymous$$onoBehaviour
{
public GameObject interceptor;
public GameObject baseBox;
public PlaceNewBaseButton baseButton;
private float interceptorspawnCounter = 0f;
private float maxinterceptorspawnLimit = 2f;
// Start is called before the first frame update
public void Start()
{
baseButton = GetComponent<PlaceNewBaseButton>();
}
public void SpawnObject()
{
Vector3 posOfBase = baseButton.baseBoxPos;
if (interceptorspawnCounter <= maxinterceptorspawnLimit)
{
Debug.Log(posOfBase);
Instantiate(interceptor,posOfBase,Quaternion.identity);
interceptorspawnCounter++;
}
else
{
Debug.Log("You can only send two interceptors at a time");
}
}
}
Answer by tkati · Jan 06, 2021 at 04:40 PM
shouldn't the code be more like baseButton.transform.position ?
edit: at this line Vector3 posOfBase = baseButton.baseBoxPos;
Tried it, nothing changed, still get the same null exception error on that line...
Your answer
Follow this Question
Related Questions
Instantiate object in front of player? 2 Answers
transform.position on Instaniated object does not match inspector 0 Answers
Updating transform.position correctly to instantiated GameObjects 0 Answers
How could I make a border around my grid world? (PIC INCLUDED) 1 Answer
The instantiated prefab doesn't fire correctly from the player 2 Answers