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. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

access a list from another class

Discussion in 'Scripting' started by scradsleep, May 10, 2020.

  1. scradsleep

    scradsleep

    Joined:
    Nov 7, 2019
    Posts:
    21
    hey all was wondering if i could please get a little help i am trying to access a list from another class i have found that the list is coming up empty in the new class i was wondering how to get it so it is still populated in its new class


    Code (CSharp):
    1.    public class UnitSelection : MonoBehaviour
    2.    {
    3.    
    4.         PlayerController playerController;
    5.         [SerializeField] SelectedUnitIcon selectedUnitIcon = null;
    6.  
    7.         private void Update()
    8.         {
    9.             Redraw();
    10.         }
    11.  
    12.         public void Redraw()
    13.         {
    14.             foreach (Transform child in transform)
    15.             {
    16.                 Destroy(child.gameObject);
    17.             }
    18.  
    19.             List<SelectedInfo> selectedObjects = playerController.GetList(); //null reference exception
    20.  
    21.             foreach (SelectedInfo item in selectedObjects)
    22.             {
    23.                 Instantiate(selectedUnitIcon);
    24.             }
    25.         }
    26.    }
    it should be pulling up my selected objects so that i can display an icon that i have set in the serialize field but i didnt realise that lists loose there contents when you transfer them across classes (im not sure if this is correct either) how do you keep it so the contents dont get removed


    cheers!
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    In your code, there's a comment about a null reference exception. You need to ensure the playerController is assigned before you access it, or do a null-check if it is allowed to be unassigned.#

    Lists are reference types and are passed by reference. I.e. you'll deal with the exact list that is in your playerController, if there is one returned. But I assume you need to fix the error first in order to see this work.
     
  3. scradsleep

    scradsleep

    Joined:
    Nov 7, 2019
    Posts:
    21

    :O{ you lost me
    im getting that its unnasigned but what would i assign it to when its all hooked up im guessing this is where i am going wrong i been trying to wrap my head around what i would do and trying so much

    would you be so kind as to show me what i would do in both examples to fix?
    i really cannot get my head around the issue

    im not sure what i am meant to assign the playerController too if it is meant to be asigned to something and im not sure what to do to do a null check if it is meant to be unnasigned as i think it is because its another script that i am refrencing

    i have tried doing playerController = GetComponent<PlayerController>(); in start() and/or awake() i tried looking at doing the null check in an if statement underneathit i dunno man im pulling my hair out somethings in coding i just dont get some of the other things i get i only been doing this for about 3 months i just dont know how to assign it like do i need to make a serialized fieild and hook up a game object cos i dont think i need to hook up any game objects to it i got 3 classes all hooked up to this the PlayerController.cs the selectedinfo.cs and the unitselection.cs i have this problem so many times and i jsut usually give up on it cos i dont get it arghh
     
  4. scradsleep

    scradsleep

    Joined:
    Nov 7, 2019
    Posts:
    21
    even links to a good video or something would be appreciated
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    It's partially a design choice how you do it, there are many ways.

    The most common way is to declare the field for the playerController as one of the following:
    Code (CSharp):
    1. public PlayerController playerController;
    2.  
    3. // or
    4. [SerializeField]
    5. private PlayerController playerController = null;
    I generally recommend to use the latter. If the former is easier for your when you need multiple scripts to communicate with each other, you can do that as well. Then just drag&drop the player controller in the inspector.

    The programmatic approach is the one you've mentioned. Though you need to make sure you call GetComponent on the GameObject (or a component on it) to which the PlayerController is attached. If your component "UnitSelection" is not attached to the same object, you won't get the reference to the PlayerController by just calling GetComponent.

    There's also FindObjectOfType, which does not require a specific object reference and "checks the whole scene" in order to find an instance for you. Whether that's good or not... Depends.
     
  6. scradsleep

    scradsleep

    Joined:
    Nov 7, 2019
    Posts:
    21

    oh my thankyou so much my friend! your to kind!!!

    i was thinking.... why is my GetComponent not working but its because its not attached to the same GameObject... thats why it dosent work... makes sense (component....) yeah i just dropped in the PlayerController to a public PlayerController playerController = null; in the inspector

    also had to add


    Code (CSharp):
    1.                 if (unit.isSelected)
    2.                 {
    3.                     var newIcon = Instantiate(selectedUnitIcon);
    4.                     newIcon.transform.SetParent(gameObject.transform);
    5.                 }
    so it would instansiate on the correct panel in the ui and tweak a few scaling issues but now it looks beautiful and my villagers blue boxes are being displayed as correctly now as you can see by my little picture hear with my lovely art of a smily face because i'm a very happy chap now!!!

    Capture.PNG

    sorry about the late reply i went to bed shortly after posting it was making my head hurt to much