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

Please help me, fix this NullReferenceException thankyou.


null ref means that the value is not set.

we need to see more code

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 () {

}
}

First…Use code tags

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.

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 ?

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.