Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Accessing variables from another class in the same script

Discussion in 'Scripting' started by DroidifyDevs, Sep 10, 2017.

  1. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Hello!

    I seem to have painted myself into a corner here. Basically, here's the code:
    Code (CSharp):
    1.     public void SpawnObject(Transform CurrentObject)
    2.     {
    3.         TileItems tiles = new TileItems();
    4.  
    5.         var spawnobj = Instantiate(CurrentObject, new Vector3(UnityEngine.Random.Range(MinXRange, MaxXRange), 0,
    6.                 UnityEngine.Random.Range(MinZRange, MaxZRange)), Quaternion.identity, transform);
    7.         spawnobj.localScale = new Vector3(0.2f, 0.2f, 0.2f);
    8.         tiles.Objects.Add(spawnobj, spawnobj.position);
    9.     }
    10. }
    11. [System.Serializable]
    12. public class TileItems
    13. {
    14.     public Dictionary<Transform, Vector3> Objects;
    15. }
    However, I'm having trouble accessing the TileItems class, as this:
    Code (CSharp):
    1. tiles.Objects.Add(spawnobj, spawnobj.position);
    Throws a null reference exception. If I do TileItems tiles = GetComponent<TileItems>(), that also throws an error as TileItems doesn't derive from Monobehaviour (and I can't derive it from Monobehaviour as I need to serialize it with JSON).

    What's stranger is that if I do this:
    Code (CSharp):
    1.         if (tiles == null)
    2.             Debug.Log("tiles is null");
    3.         else
    4.             Debug.Log("tiles isn't null!");
    It shows "tiles isn't null".

    Any ideas? Thanks.
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Hey, you basically have the code. You would just have to make a decliration of TileItems to access the components inside

    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class TileItems
    4. {
    5.     public Dictionary<Transform, Vector3> Objects;
    6. }
    7. public TileItems tileItems;
    8.  

    Code (CSharp):
    1.   tileItems.Objects.Add(spawnobj,spawnobj.position);
     
  3. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Like this?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [System.Serializable]
    5. public class TileItems
    6. {
    7.     public Dictionary<Transform, Vector3> Objects;
    8.     public string TestString;
    9. }
    10.  
    11. public class TilePopulatorV2 : MonoBehaviour
    12. {
    13.     public float MinXRange;
    14.     public float MaxXRange;
    15.     public float MinZRange;
    16.     public float MaxZRange;
    17.     public float TileWidth;
    18.     public TileItems tiles;
    19.     public JSONSaver SaveScript;
    20.     // Use this for initialization
    21.     void Start()
    22.     {
    23.         TileWidth = transform.GetComponent<Renderer>().bounds.extents.x;
    24.     }
    25.  
    26.     public void SpawnObject(Transform CurrentObject)
    27.     {
    28.         TileWidth = transform.GetComponent<Renderer>().bounds.extents.x;
    29.         MinXRange = transform.position.x - (TileWidth);
    30.         MaxXRange = transform.position.x + (TileWidth);
    31.         MinZRange = transform.position.z - (TileWidth);
    32.         MaxZRange = transform.position.z + (TileWidth);
    33.         var spawnobj = Instantiate(CurrentObject, new Vector3(UnityEngine.Random.Range(MinXRange, MaxXRange), 0,
    34.                 UnityEngine.Random.Range(MinZRange, MaxZRange)), Quaternion.identity, transform);
    35.         spawnobj.localScale = new Vector3(0.2f, 0.2f, 0.2f);
    36.         if (tiles == null)
    37.             Debug.Log("tiles is null");
    38.         else
    39.             Debug.Log("tiles isn't null!");
    40.         tiles.TestString = "hi";
    41.         Debug.Log("transform name: " + spawnobj.transform.name);
    42.         Debug.Log("position: " + spawnobj.transform.position);
    43.         tiles.Objects.Add(spawnobj.transform, spawnobj.position);
    44.     }
    45. }
    Adding the string works and all the Debug.Logs fire, but the last line is still an error :/
    So close:mad:
     
  4. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Ah, figured it out we both dun goofed!

    Code (CSharp):
    1.   public Dictionary<Transform, Vector3> Objects = new Dictionary<Transform, Vector3>();
    The dictionary is never being created/instantiated (similar to lists)

    This should work!
     
  5. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Oh now I feel stupid. I really need to learn why some things need to be instantiated (like this dictionary), and some things don't (for example, lists).

    Thank you!
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I think it's public and serialized fields (for collections) .. ones that unity can serialize (also?) that will be instantiated implicitly.