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

Question Why an initialized attribute still be null in a member method?

Discussion in 'Scripting' started by Sakurinh, Dec 12, 2022.

  1. Sakurinh

    Sakurinh

    Joined:
    Jul 26, 2017
    Posts:
    15
    My code here:
    Code (CSharp):
    1. public class SetDefender : MonoBehaviour
    2. {
    3.     [SerializeField] GameObject gridOnField;
    4.     private void Awake() {
    5.         print("grid="+gridOnField);
    6.     }
    7.     public Vector2 GetPointer() {
    8.         Vector2 clickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    9.         Vector2 worldClickPos = Camera.main.ScreenToWorldPoint(clickPos);
    10.         Vector2 spawnPoint = new Vector2(Mathf.Clamp(Mathf.RoundToInt(worldClickPos.x),1,9),
    11.                                                 Mathf.Clamp(Mathf.RoundToInt(worldClickPos.y),1,5));
    12.         return spawnPoint;
    13.     }
    14.     public void SpawnDefender( GameObject defender) {
    15.         print("grid="+gridOnField);
    16.         if(gridOnField.GetComponent<Grid>().GetTileAccess(GetPointer())!=null) {
    17.             print("accessing");
    18.             Instantiate(defender,GetPointer(),Quaternion.identity);
    19.             gridOnField.GetComponent<Grid>().TakenTile(GetPointer());
    20.         }
    21.     }
    22. }
    When I call SpawnDefender from other class, the second "print" line gives NULL, even I did drag and drop the grid object in the field. Why? Thank you for reading and answering. Pls ask me anything if the question is not clear enough.
     
  2. Sakurinh

    Sakurinh

    Joined:
    Jul 26, 2017
    Posts:
    15
    I'm sorry, I found the problem. The one that called Spawndefender was a prefab, which can not assign an in-field GameObject as SerializeField. Therefore, I must assign it by a call inside SpawnDefender.
     
    Bunny83 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    Good find. Scene stuff can refer to prefabs / assets, but never the other way around.

    Also, just as a heads up about hairy code like this:

    and

    You really want to avoid that kinda code!

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums