Search Unity

NullRefferenceException error I can't seem to figure out

Discussion in 'Scripting' started by Hoespilaar, Jun 23, 2019.

  1. Hoespilaar

    Hoespilaar

    Joined:
    Jun 23, 2019
    Posts:
    15
    I am making a game where i need to change the alpha of UIimages wich are created from a prefab. It worked perfectly until I made a script to make them all become transparent as soon as the game starts.

    I get this error:

    NullReferenceException: Object reference not set to an instance of an object
    UIPlaceBlockSelectorObject.ChangeState (System.Int32 NewState) (at Assets/Scripts/UIPlaceBlockSelectorObject.cs:47)
    UIPlaceBlockBackGround.CreateAllSprites () (at Assets/Scripts/UIPlaceBlockBackGround.cs:57)
    UIPlaceBlockBackGround.Start () (at Assets/Scripts/UIPlaceBlockBackGround.cs:23)


    and the first script it redirects towards, wich is on the UIimage (UIPlaceBlockSelectorObject):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class UIPlaceBlockSelectorObject : MonoBehaviour
    7. {
    8.     public int state; // 0 = nothing 1 = clickable 2 = selected
    9.     public float movespeed;
    10.  
    11.     private Image _imageComponent;
    12.     private RectTransform _RectTransform, Parent;
    13.     private float MoveDistanceX, nextPosition;
    14.     private bool Neg;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         Parent = GameObject.Find("PlaceBlockMaster").transform as RectTransform;
    20.         _RectTransform = gameObject.transform as RectTransform;
    21.  
    22.         _imageComponent = _RectTransform.gameObject.GetComponent<Image>();
    23.  
    24.         MoveHorizontal(true, 1);
    25.  
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.      
    32.     }
    33.  
    34.     public void UpdateSize()
    35.     {
    36.         _RectTransform.sizeDelta = new Vector2(Parent.sizeDelta.y * 3/5, Parent.sizeDelta.y * 3/5);
    37.     }
    38.  
    39.     public void ChangeState(int NewState)
    40.     {
    41.      
    42.         if (NewState == 0)
    43.         {
    44.             _imageComponent.color = new Color(_imageComponent.color.r, _imageComponent.color.g, _imageComponent.color.b, 0);
    45.         } else if (NewState == 1)
    46.         {
    47.             _imageComponent.color = new Color(_imageComponent.color.r, _imageComponent.color.g, _imageComponent.color.b, 0.5f);
    48.         } else if (NewState == 2)
    49.         {
    50.             _imageComponent.color = new Color(_imageComponent.color.r, _imageComponent.color.g, _imageComponent.color.b, 1);
    51.         }
    52.     }
    53.  
    54.     public void MoveHorizontal(bool Left, int spaces)
    55.     {
    56.         MoveDistanceX = Screen.width / 7;
    57.  
    58.         if (Left)
    59.         {
    60.             nextPosition = _RectTransform.position.x - MoveDistanceX;
    61.         } else
    62.         {
    63.             nextPosition = _RectTransform.position.x + MoveDistanceX;
    64.         }
    65.  
    66.         if (_RectTransform.position.x >= nextPosition)
    67.         {
    68.             Neg = true;
    69.         }
    70.         else
    71.         {
    72.             Neg = false;
    73.         }
    74.  
    75.         if (spaces > 1)
    76.         {
    77.  
    78.         }
    79.  
    80.         StartCoroutine("_moveTowards");
    81.     }
    82.  
    83.     private IEnumerator _moveTowards()
    84.     {
    85.  
    86.         for (int i = 0; nextPosition != _RectTransform.position.x; i++)
    87.         {
    88.  
    89.             if (Neg)
    90.             {
    91.                 _RectTransform.position = new Vector3(_RectTransform.position.x - movespeed, _RectTransform.position.y, 0);
    92.  
    93.                 if (_RectTransform.position.x <= nextPosition)
    94.                 {
    95.                     _RectTransform.position = new Vector3(nextPosition, _RectTransform.position.y, 0);
    96.                 }
    97.             } else
    98.             {
    99.                 _RectTransform.position = new Vector3(_RectTransform.position.x + movespeed, _RectTransform.position.y, 0);
    100.  
    101.                 if (_RectTransform.position.x >= nextPosition)
    102.                 {
    103.                     _RectTransform.position = new Vector3(nextPosition, _RectTransform.position.y, 0);
    104.                 }
    105.             }
    106.  
    107.             yield return new WaitForFixedUpdate();
    108.         }
    109.     }
    110. }
    111.  
    and the other script, wich is attached to the parent of all UIimages (UIPlaceBlockBackGround):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class UIPlaceBlockBackGround : MonoBehaviour
    7. {
    8.     public GameObject BaseImage;
    9.  
    10.     private RectTransform _transform;
    11.     private GameObject EditorMaster;
    12.     private GameObject[] _Prefabs;
    13.     private Vector2 PrevScreen;
    14.  
    15.     void Start()
    16.     {
    17.         _transform = gameObject.transform as RectTransform;
    18.  
    19.         _transform.position = new Vector3(0, 0, 0);
    20.      
    21.         EditorMaster = GameObject.FindGameObjectWithTag("GameController");
    22.  
    23.         CreateAllSprites();
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         if (PrevScreen == new Vector2(Screen.width, Screen.height)) {
    29.             _transform.sizeDelta = new Vector2(Screen.width, Screen.height / 5);
    30.  
    31.             UpdateAllChildrenSize();
    32.         }
    33.  
    34.         PrevScreen = new Vector2(Screen.width, Screen.height);
    35.     }
    36.  
    37.     public void Activate(bool deactivate)
    38.     {
    39.         gameObject.SetActive(deactivate);
    40.     }
    41.  
    42.     private void CreateAllSprites()
    43.     {
    44.         _Prefabs = EditorMaster.GetComponent<PlaceBlock>().prefabs;
    45.  
    46.         BaseImage.GetComponent<Image>().sprite = GetObjectSprite(0);
    47.  
    48.         for (int i = 0; i != _Prefabs.Length - 1; i++)
    49.         {
    50.             GameObject NewImage = Instantiate(BaseImage, _transform);
    51.  
    52.             NewImage.GetComponent<Image>().sprite = GetObjectSprite(i + 1);
    53.  
    54.             if (i < 1) {
    55.                 if (NewImage.GetComponent<UIPlaceBlockSelectorObject>() != null)
    56.                 {
    57.                     NewImage.GetComponent<UIPlaceBlockSelectorObject>().ChangeState(1);
    58.                 }
    59.  
    60.                 if (i == 0)
    61.                 {
    62.  
    63.                 }
    64.             } else
    65.             {
    66.                 NewImage.GetComponent<UIPlaceBlockSelectorObject>().ChangeState(0);
    67.             }
    68.         }
    69.     }
    70.  
    71.     private Sprite GetObjectSprite(int Pos)
    72.     {
    73.         return _Prefabs[Pos].GetComponent<SpriteRenderer>().sprite;
    74.     }
    75.  
    76.     private void UpdateAllChildrenSize()
    77.     {
    78.         UIPlaceBlockSelectorObject[] _Children = gameObject.GetComponentsInChildren<UIPlaceBlockSelectorObject>();
    79.  
    80.         for (int i = 0; i != _Children.Length; i++)
    81.         {
    82.             _Children[i].UpdateSize();
    83.         }
    84.     }
    85. }
    86.  
    I have been plucking this error apart for almost 2 weeks, but I can 't seem to fix it.

    I hope you can help me, and if you see any improvements to my code, you are welcome to help me!
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    Pretty simple. Null means "nothing". Null reference error means that you are trying to access an object from a reference variable, but that variable is not set to an object. Instead it's just set to nothing at all. Look at 47 like the error message says. there is only one object referenced on that line.
     
  3. Hoespilaar

    Hoespilaar

    Joined:
    Jun 23, 2019
    Posts:
    15
    Well that was a stupid mistake... But I fixed it, so thank you! Hope I won 't ever do that again!
     
  4. DungeonBrickStudios

    DungeonBrickStudios

    Joined:
    Jun 9, 2015
    Posts:
    69
    Null ref is probably the most common error in programming, we all do it all the time experienced and inexperienced alike :D