Search Unity

NullReferenceException: Object reference not set to an instance of an object CharObject.Select () (a

Discussion in 'Scripting' started by JMConsignado, Apr 9, 2017.

?

Can I Ask some suggestions how can I fix it? Thankyou

  1. Codes

    2 vote(s)
    100.0%
  2. Text

    1 vote(s)
    50.0%
Multiple votes are allowed.
  1. JMConsignado

    JMConsignado

    Joined:
    Apr 9, 2017
    Posts:
    2
    Please help me, fix this NullReferenceException thankyou. upload_2017-4-8_20-43-2.png
    upload_2017-4-8_20-43-59.png
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    null ref means that the value is not set.

    we need to see more code
     
  3. JMConsignado

    JMConsignado

    Joined:
    Apr 9, 2017
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class CharObject : MonoBehaviour
    {

    public char character;
    public Text text;
    public Image image;
    public RectTransform rectTransform;
    public int index;

    [Header("Appearance")]
    public Color normalColor;
    public Color selectedColor;

    bool isSelected = false;

    public CharObject Init (char c)
    {
    character = c;
    text.text = c.ToString();
    gameObject.SetActive(true);
    return this;
    }

    public void Select()
    {
    isSelected = !isSelected;

    image.color = isSelected ? selectedColor : normalColor;
    if (isSelected)
    {
    Scramble.main.Select(this);
    }
    else
    {
    Scramble.main.UnSelect();
    }

    }


    // Use this for initialization
    void Start () {

    }

    // Up date is called once per frame
    void Update () {

    }
    }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    First...Use code tags
    https://forum.unity3d.com/threads/using-code-tags-properly.143875/#post-3020744

    Second...Don't add polls that serve no purpose...

    Now, as I'm lazy and not going to count lines to figure out which line is 36, and your screenshot doesn't show line numbers, the best thing I can tell you is use the tags and we can maybe help you better. But, null errors are generally easy to fix. Look at the line it references and start working through it. If your line is the Scramble.main, then is Scramble null? Is Scramble.main null?

    You can test these with debug.log(Scramble) and debug.log(Scramble.main).

    My guess is whatever Scramble.main is is probably null, but this is a guess.
     
  5. gizmoytgr

    gizmoytgr

    Joined:
    May 26, 2019
    Posts:
    1
    my problem same

    public void Select ()
    {
    //test
    isSelected = !isSelected;
    image.color = isSelected ? selectedColor : normalColor;
    if (isSelected)
    {
    WordScramble.main.Select (this);
    } else
    {
    WordScramble.main.UnSelect ();
    }


    }

    at the "image.color = isSelected ? selectedColor : normalColor;"
    how to solve this ?
     
  6. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Please do not resurrect a thread like this. It is best to start your own so that there is no noise from other posts.

    Looks like the only thing on that line which can be null is 'image'. Make sure it is assigned in the inspector.