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. Dismiss Notice

Question Need a little help figuring out an "object not set to an instance" error

Discussion in 'Scripting' started by scr33ner, Jan 10, 2023.

  1. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    187
    I'm getting an error on line 65: txtSpeedometer.text = distanceTraveled.ToString("000");

    The code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using TMPro;
    6.  
    7. public class SpeedoMeter : MonoBehaviour
    8. {
    9.     #region Public Variables
    10.  
    11.     public float distanceTraveled;
    12.     public float speed;
    13.     public float unitSecs = 1.0f; // units/sec
    14.  
    15.     // ship forward velocity variables
    16.     public bool isMovingForward;
    17.     public bool isMovingBackward;
    18.     public Vector3 LastPOS;
    19.     public Vector3 NextPOS;
    20.  
    21.     [Header("RigidBodies")]
    22.     public Rigidbody rbPlayerShip;         //used to calculate velocity
    23.  
    24.     [Header("Object Transforms")]
    25.     public Transform playerPos;
    26.     [SerializeField] public TextMeshProUGUI txtSpeedometer;
    27.     //
    28.     #endregion
    29.  
    30.     // Start is called before the first frame update
    31.     void Awake()
    32.     {
    33.         rbPlayerShip = GetComponent<Rigidbody>();
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.         //get the ship's speed
    40.         Speedometer(); // <--call this in Racemanager Update
    41.     }
    42.  
    43.     void LateUpdate()
    44.     {      
    45.         SpeedToString();
    46.         //orientationCheckScript.CheckOrientation();
    47.         //checkForwardMovement();
    48.     }
    49.     //check to make sure ship is moving forward
    50.     public void checkForwardMovement()
    51.     {
    52.         //Debug.Log("checkForwardMovement");
    53.  
    54.     }
    55.  
    56.     //speedometer
    57.     public void Speedometer()
    58.     {
    59.         distanceTraveled = rbPlayerShip.GetComponent<Rigidbody>().velocity.magnitude;
    60.         speed = Time.deltaTime * distanceTraveled;
    61.  
    62.         Debug.Log("Distancetraveled: " + distanceTraveled);
    63.         //Debug.Log("Speed: " + speed);
    64.        
    65.         txtSpeedometer.text = distanceTraveled.ToString("000");
    66.  
    67.     }
    68.     public void SpeedToString()
    69.     {
    70.  
    71.         if (distanceTraveled > 1)
    72.         {
    73.            //txtSpeedometer.text = distanceTraveled.ToString("000");
    74.             //txtSpeedometer.text = speed.ToString("000");          
    75.             //Debug.Log("to string");
    76.         }
    77.         //Debug.Log(speed);
    78.     }
    79.  
    80. }
    81.  
    Not sure what's happening, it's assigned in the inspector:
    Untitled-1.png
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Most likely you have another copy of the SpeedoMeter script in your scene, and on that copy, the text object is not assigned.
     
    scr33ner likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,768
  4. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    187
    Perfect, thank you so much!