I have debugged this for what seems like ages, but I still do not understand this:
In one script, BuildingManager, I instantiate a new GameObject called MainHouse and I collect x and z data from it. In those same lines, I attempt to change the public floats in MenuManager to be these coordinates. In debugging, in the BuildingManager script the coordinates come out as the correct coordinates, being where the player put down their starting house. However, when debugging ANYWHERE in the MenuManager, the coordinates turn out to be 0,0. One other piece of information: Upon changing the coordinates in the House prefab, debugging the coordinates will return the coordinates of the prefab, as opposed to the House the player spawns in by clicking.
Thanks for the help. ![]()
Here is BuildingManager.cs:
using UnityEngine;
using System.Collections;
public class BuildingManager : MonoBehaviour {
public bool starterBuildingBool;
public GameObject house;
public GameObject closeButton;
public GameObject menuManager;
void Start () {
starterBuildingBool = true;
}
void Update () {
if (starterBuildingBool) {
StartingBuilding ();
}
}
void StartingBuilding() {
var menuScript = menuManager.GetComponent<MenuManager> ();
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast (ray, out hitInfo) && menuScript.menuClosed) {
GameObject ourHitObject = hitInfo.collider.transform.gameObject;
//Debug.Log ("Raycast hit: " + hitInfo.collider.transform.parent.name);
if (Input.GetMouseButtonDown (0)) {
Vector3 pos = new Vector3 (ourHitObject.transform.position.x,
ourHitObject.transform.position.y,
ourHitObject.transform.position.z);
GameObject MainHouse = (GameObject)Instantiate (house, pos, Quaternion.identity);
MainHouse.name = "Home";
menuScript.xHomePos = MainHouse.transform.position.x;
Debug.Log (MainHouse.transform.position.x);
menuScript.zHomePos = MainHouse.transform.position.z;
Debug.Log (MainHouse.transform.position.z);
starterBuildingBool = false;
}
}
}
}
And here is MenuManager.cs:
using UnityEngine;
using System.Collections;
public class MenuManager : MonoBehaviour {
public GameObject text;
public GameObject button;
public GameObject homeButton;
public GameObject canvas;
public bool menuClosed;
public float xHomePos;
public float zHomePos;
void Start() {
menuClosed = false;
}
public void OnStartGame() {
Destroy (text);
Destroy (button);
menuClosed = true;
GameObject hb = (GameObject)Instantiate (
homeButton, new Vector3 (0,0,0), Quaternion.identity
);
hb.transform.SetParent (canvas.transform, false);
hb.transform.localScale = new Vector3(1,1,1);
hb.transform.localRotation = new Quaternion ();
hb.transform.localPosition = new Vector3 (178.7f,-301.2f,-7.3f);
hb.name = "HomeButton";
}
public void HomeButtonClick() {
Camera cam = Camera.main;
Debug.Log (xHomePos);
Debug.Log (zHomePos);
cam.transform.position = new Vector3 (xHomePos, 8, zHomePos - 2);
}
}