Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Question [SOLVED]How to get values from other scripts?

Discussion in 'Scripting' started by meticulouswizardry, Jan 5, 2021.

  1. meticulouswizardry

    meticulouswizardry

    Joined:
    Jan 5, 2021
    Posts:
    3
    I'm starting on unity and I have some very simple grid code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5.  
    6.  
    7. public class GridManager : MonoBehaviour
    8. {
    9.     private float[,] Grid;
    10.     private int Vertical, Horizontal, Columns, Rows;
    11.     public GameObject tile;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         Vertical = (int) Camera.main.orthographicSize;
    17.         Horizontal = Vertical * (Screen.width / Screen.height);
    18.         Columns = Horizontal * 2;
    19.         Rows = Vertical * 2;
    20.         Grid = new float[Columns, Rows];
    21.         for (int i = 0; i < Columns; i++)
    22.         {
    23.             for (int j = 0; j < Rows; j++)
    24.             {
    25.                 Grid[i, j] = Random.Range(0.0f, 1.0f);
    26.                 SpawnTile(i,j, Grid[i,j]);
    27.             }
    28.         }
    29.     }
    30.  
    31.     private void SpawnTile(int x, int y, float value)
    32.     {
    33.         Instantiate(tile);
    34.         tile.transform.position = new Vector3(x - (Horizontal - 0.5f), y - (Vertical - 0.5f));
    35.         Debug.Log(tile.GetComponent<tile>().objName.ToString());
    36.    
    37.  
    38.  
    39.     }
    40.     // Update is called once per frame
    41.     void Update()
    42.     {
    43.      
    44.     }
    45. }
    46.  
    I have my tile prefab attached to my GridManager game object in unity and all is going well except I cannot log objName. I'm not sure why the code to access the tile's variable isn't working. Here is the tile script attached to my tile prefab:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6. public class tile : MonoBehaviour
    7. {
    8.     public Sprite sprite;
    9.  
    10.     public int objName;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         objName = Random.Range(0, 990000000);
    15.    
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.      
    23.     }
    24. }
    25.  
    Again, I am new to unity and from what I've read
    Code (CSharp):
    1. tile.GetComponent<tile>().objName.ToString()
    should be how I access other scripts variables. It would be very helpful if you could point out where I am messing up. Thank you in advance.
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    are there any errors in the console?

    is :
    Code (CSharp):
    1. public GameObject tile;
    set and not null?
     
  3. Hyarch

    Hyarch

    Joined:
    Jan 22, 2015
    Posts:
    34
    Why not set the objName From your Spawn tile method

    Code (CSharp):
    1. tile.gameobject.name = Random.Range(0, 990000000);
    or

    Code (CSharp):
    1. tile.gameobject.GetComponent<tile>().objName = Random.Range(0, 990000000);
    also you should instantiate properly

    Code (CSharp):
    1. GameObject tile = Instantiate(tile) as GameObject;
    finished
    Code (CSharp):
    1.  
    2.  
    3. public transform prefab;
    4.     private void SpawnTile(int x, int y, float value)
    5.     {
    6. Vector3 newPosition = new Vector3(x - (Horizontal - 0.5f), y - (Vertical - 0.5f));
    7.    
    8.  GameObject tile = Instantiate(prefab, newPosition , Quaternion.identity) as GameObject;
    9.  
    10.  
    11.         tile.GetComponent<tile>().objName = Random.Range(0, 990000000);
    12.  
    13.         Debug.Log(tile.GetComponent<tile>().objName.ToString());
    14.  
    15.  
    16.  
    17.     }
    18.  
    if your using visual studio use file/open folder navigate to your unity project folder and it should load everything as a project and it will give you more help on things that are wrong.
     
    Last edited: Jan 5, 2021
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Put the objName line in Awake(), not Start. If you call another script from Start(), you can't be certain the other script's Start() has been called yet.

    Also,

    Code (CSharp):
    1. private void SpawnTile(int x, int y, float value)
    2.     {
    3.         GameObject temp = Instantiate(tile);
    4.         temp.transform.position = new Vector3(x - (Horizontal - 0.5f), y - (Vertical - 0.5f));
    5.         Debug.Log(temp.GetComponent<tile>().objName);
    6.     }
    Notice I used temp to get the returned instantiated object, whereas you just instantiated it and then it's lost...but you still accessed it with "tile" and it works...can anyone speak to this? Doesn't seem like it's the right way to deal with instantiated objects, but I might be missing something.
     
    Hyarch likes this.
  5. Hyarch

    Hyarch

    Joined:
    Jan 22, 2015
    Posts:
    34

    Correct he must be getting errors tile gamobject not found

    or its some how trying to grab it from his prefab value thats not an instantiated yet might be bug not calling an exception
     
    seejayjames likes this.
  6. meticulouswizardry

    meticulouswizardry

    Joined:
    Jan 5, 2021
    Posts:
    3
    Thank you all for your help!
     
    Hyarch and seejayjames like this.