-2
\$\begingroup\$

For some reason, the Unity console shows an "Object reference not set to an instance of an object error" when I run this line of code:

Debug.Log("MESH IS " + CharacterChange.instance.Player);

Why am I getting this error?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterChange : MonoBehaviour
{
    public static CharacterChange instance;

    public static GameObject player1dub;
    public GameObject Player;
    public GameObject orangeShown;
    public GameObject grapesShown;
    public GameObject appleShown;
    public GameObject bananaShown;
    public int num;
    public Transform playerTransform;
    public Transform orangeTransform;
    public Transform grapesTransform;
    public Transform appleTransform;
    public Transform bananaTransform;
    public Transform orangeHitPoint;
    public Transform grapesHitPoint;
    public Transform remyHitPoint;
    public Transform appleHitPoint;
    public Transform bananaHitPoint;
    public Transform secondplayerhit;
    public Transform orangeCameraMovement;
    public Transform grapesCameraMovement;
    public Transform remyCameraMovement;
    public Transform appleCameraMovement;
    public Transform bananaCameraMovement;
    public Transform secondplayercamera;
    PlayerMotion playerMotionScript;
    CameraManager cameraManagerScript;
    Gun gunScript;
    Animator myAnimator;
    Timer timerscript;
    ScalingOfModels scalingScript;
    AiSensor sensorScript;
    //public SpawnerCharacter characterSpawn;

    public bool scaleBool;
    public bool remyScaleBool;
    public Vector3 endScale;
    public Vector3 startScale;
    public float timeDuration;
    public float elapsedTime;

    void Start()
    {
        playerMotionScript = GetComponentInParent<PlayerMotion>();
        cameraManagerScript = FindObjectOfType<CameraManager>();
        gunScript = FindObjectOfType<Gun>();
        scalingScript = GetComponent<ScalingOfModels>();
        myAnimator = GetComponent<Animator>();
        timerscript = FindObjectOfType<Timer>();
        sensorScript = GetComponent<AiSensor>();
        startScale = playerMotionScript.selectedPlayer.transform.localScale;
    }

    public IEnumerator RemyChangeEnumerator()
    {
        if (playerMotionScript.selectedPlayer != Player)
        {
            playerMotionScript.selectedPlayer.SetActive(false);
            elapsedTime += Time.deltaTime;
            float complete = elapsedTime / timeDuration;
            playerMotionScript.selectedPlayer.transform.localScale = Vector3.Lerp(endScale, startScale, complete);
            playerTransform.position = playerMotionScript.selectedPlayer.transform.position;
            Player.SetActive(true);
            playerMotionScript.allHitPoints = remyHitPoint;
            playerMotionScript.selectedPlayer = Player;
            playerMotionScript.HandlePlayerChange();
            cameraManagerScript.SetCameraMovement(remyCameraMovement);
            yield return null;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        Destroy(other.gameObject);

        if (other.gameObject.tag == "OrangeCollision" && playerMotionScript.selectedPlayer != orangeShown)
        {
            ChangeCharacter(orangeShown, orangeTransform, orangeHitPoint, orangeCameraMovement);
        }
        else if (other.gameObject.tag == "GrapesCollision" && playerMotionScript.selectedPlayer != grapesShown)
        {
            ChangeCharacter(grapesShown, grapesTransform, grapesHitPoint, grapesCameraMovement);
        }
        else if (other.gameObject.tag == "AppleCollision" && playerMotionScript.selectedPlayer != appleShown)
        {
            ChangeCharacter(appleShown, appleTransform, appleHitPoint, appleCameraMovement);
        }
        else if (other.gameObject.tag == "BananaCollision" && playerMotionScript.selectedPlayer != bananaShown)
        {
            ChangeCharacter(bananaShown, bananaTransform, bananaHitPoint, bananaCameraMovement);
        }
    }

    private void ChangeCharacter(GameObject newCharacter, Transform newTransform, Transform newHitPoint, Transform newCameraMovement)
    {
        playerMotionScript.selectedPlayer.SetActive(false);
        elapsedTime += Time.deltaTime;
        float complete = elapsedTime / timeDuration;
        playerMotionScript.selectedPlayer.transform.localScale = Vector3.Lerp(endScale, startScale, complete);

        newTransform.position = playerMotionScript.selectedPlayer.transform.position;
        newCharacter.SetActive(true);

        playerMotionScript.allHitPoints = newHitPoint;
        playerMotionScript.selectedPlayer = newCharacter;
        playerMotionScript.HandlePlayerChange();

        cameraManagerScript.SetCameraMovement(newCameraMovement);

        timerscript.linewait = 5;
        timerscript.timerBar.fillAmount = 1;

        newCharacter.GetComponent<ScalingOfModels>().scale = true;
    }
}

The above is my character change script, and below is my select button script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SelectButton : MonoBehaviour
{
    public Button selectButton;
    public GameObject[] characterPrefabs;
    public CharactersManager managerScript;
    // Start is called before the first frame update
    void Start()
    {
    }

    //Update is called once per frame
    void Update()
    {
        
    }

    public void CharactersSelect()
    {
        if (managerScript.imageCollection[0])
        {
            Debug.Log("MESH IS " + CharacterChange.instance.Player);
        }
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ Is your character change in the scene hierarchy or did you only create the script? \$\endgroup\$ Commented Jul 12, 2024 at 6:42
  • 5
    \$\begingroup\$ If you want people to solve your problem for you, then you need to properly describe what your problem is. The most likely reason why this statement would "give you null" is because CharacterChange.Player is null in this situation. You probably expect it to be something else than null. Why do you think that it would be something different? Please explain, so we can follow your train of thought and tell you where it went on the wrong track. \$\endgroup\$ Commented Jul 12, 2024 at 7:54
  • \$\begingroup\$ Have you read past Q&A about how to troubleshoot null reference errors in Unity? \$\endgroup\$ Commented Jul 13, 2024 at 10:44
  • \$\begingroup\$ Player isn't assigned, or the instance isn't assigned. It looks like you're trying to make CharacterChange a singleton, but you never assign what the instance of it is, and since it's a static variable, it's not going to reference itself without assignment. \$\endgroup\$ Commented Jul 15, 2024 at 18:50

1 Answer 1

1
\$\begingroup\$

An "Object reference not set to an instance of an object" error is thrown when we try to access a member of an object (using the "." operator on it) that is referenced by a variable, but that variable is currently pointing at null, not a real object.

In this line, there are three dot operators:

Debug.Log("MESH IS " + CharacterChange.instance.Player);
// 1⬆️                              2⬆️      3⬆️

The first and second are accessing static members of classes (Debug and CharacterChange, respectively) - not an object referenced by a variable. So they can't be the source of this error.

That means the error must be coming from the last dot operator:

instance.Player 

Which means the variable instance is currently holding the value null - it has not been assigned to point at a valid instance of the CharacterChange class.

Our next step is to search the code for where we assign a value to this variable, to understand what's preventing that code from running, or assigning the wrong value.

When I search the code you've provided, I find no code assigns a value to instance. It could be that you forgot to write the code that assigns this (say in Start or Awake or OnEnable), or it's assigned somewhere else in a code file you didn't show us.

Hopefully that gives you the lead you need to solve this problem.

\$\endgroup\$
6
  • \$\begingroup\$ HI DMGregory i have given the above two scripts so any point that you can give me to make it. \$\endgroup\$ Commented Jul 14, 2024 at 2:47
  • \$\begingroup\$ I'm not sure what you're asking me. You have not altered the code shown in the question since the last version I read, which is the version I refer to in this answer. Do you have new information you want to share, or a clarifying question to ask? \$\endgroup\$ Commented Jul 14, 2024 at 2:54
  • \$\begingroup\$ I mean the above two scripts. \$\endgroup\$ Commented Jul 14, 2024 at 3:39
  • \$\begingroup\$ What about them? \$\endgroup\$ Commented Jul 14, 2024 at 11:22
  • \$\begingroup\$ Give me the suggestion that how can i make the character selection screen i have asked another question answer that question with derails \$\endgroup\$ Commented Jul 16, 2024 at 6:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.