Search Unity

Passing a float from one script into another seems to make the floats 0.

Discussion in 'Scripting' started by Logan_JaCav, Jul 24, 2017.

  1. Logan_JaCav

    Logan_JaCav

    Joined:
    May 21, 2016
    Posts:
    1
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BuildingManager : MonoBehaviour {
    5.  
    6.     public bool starterBuildingBool;
    7.     public GameObject house;
    8.     public GameObject closeButton;
    9.     public GameObject menuManager;
    10.  
    11.     void Start () {
    12.    
    13.         starterBuildingBool = true;
    14.  
    15.     }
    16.    
    17.  
    18.     void Update () {
    19.  
    20.         if (starterBuildingBool) {
    21.             StartingBuilding ();
    22.         }
    23.     }
    24.        
    25.     void StartingBuilding() {
    26.  
    27.         var menuScript = menuManager.GetComponent<MenuManager> ();
    28.  
    29.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    30.  
    31.         RaycastHit hitInfo;
    32.  
    33.         if (Physics.Raycast (ray, out hitInfo) && menuScript.menuClosed) {
    34.             GameObject ourHitObject = hitInfo.collider.transform.gameObject;
    35.  
    36.             //Debug.Log ("Raycast hit: " + hitInfo.collider.transform.parent.name);
    37.  
    38.             if (Input.GetMouseButtonDown (0)) {
    39.  
    40.                 Vector3 pos = new Vector3 (ourHitObject.transform.position.x,
    41.                                             ourHitObject.transform.position.y,
    42.                                             ourHitObject.transform.position.z);
    43.  
    44.                 GameObject MainHouse = (GameObject)Instantiate (house, pos, Quaternion.identity);
    45.  
    46.                 MainHouse.name = "Home";
    47.  
    48.                 menuScript.xHomePos = MainHouse.transform.position.x;
    49.                 Debug.Log (MainHouse.transform.position.x);
    50.                 menuScript.zHomePos = MainHouse.transform.position.z;
    51.                 Debug.Log (MainHouse.transform.position.z);
    52.  
    53.  
    54.                 starterBuildingBool = false;
    55.             }
    56.  
    57.         }
    58.     }
    59. }
    And here is MenuManager.cs:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MenuManager : MonoBehaviour {
    5.  
    6.     public GameObject text;
    7.     public GameObject button;
    8.     public GameObject homeButton;
    9.     public GameObject canvas;
    10.     public bool menuClosed;
    11.     public float xHomePos;
    12.     public float zHomePos;
    13.  
    14.     void Start() {
    15.         menuClosed = false;
    16.     }
    17.  
    18.     public void OnStartGame() {
    19.  
    20.         Destroy (text);
    21.         Destroy (button);
    22.         menuClosed = true;
    23.  
    24.         GameObject hb = (GameObject)Instantiate (
    25.  
    26.             homeButton, new Vector3 (0,0,0), Quaternion.identity
    27.         );
    28.  
    29.         hb.transform.SetParent (canvas.transform, false);
    30.         hb.transform.localScale = new Vector3(1,1,1);
    31.         hb.transform.localRotation = new Quaternion ();
    32.         hb.transform.localPosition = new Vector3 (178.7f,-301.2f,-7.3f);
    33.         hb.name = "HomeButton";
    34.  
    35.     }
    36.  
    37.     public void HomeButtonClick() {
    38.  
    39.         Camera cam = Camera.main;
    40.  
    41.         Debug.Log (xHomePos);
    42.         Debug.Log (zHomePos);
    43.  
    44.         cam.transform.position = new Vector3 (xHomePos, 8, zHomePos - 2);
    45.     }
    46. }
    47.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    This all seems reasonable. About the only thing I can think of is if you perhaps have TWO GameObjects with MenuManagers on them, and you have the wrong one dragged into the menuManager field??