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!