Search Unity

"Object reference not set to an instance of an object" but all of my variables are declared properly

Discussion in 'Getting Started' started by Zakariyya_Madry, Mar 17, 2022.

  1. Zakariyya_Madry

    Zakariyya_Madry

    Joined:
    Jan 15, 2019
    Posts:
    1
    So my project is a simulation of a solar system, and I have two scripts for each celestial body. One governs all of the physics of where the object goes and how large/small it is (script A), while the other recives inputs from the user (the user can change the mass/density/velocity/etc. of the planet) and calculates the variables that the first script needs (radius, mass, etc.) (script B). I've also had to scale down the numbers logarithmically and with division, because otherwise they're too big to be floats.

    My problem is when I try to call the variables from script B to script A, Unity gives me these errors:
    upload_2022-3-16_20-34-17.png

    Here is the script A (Planet):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [ExecuteInEditMode]
    6. [RequireComponent(typeof(Rigidbody))]
    7. public class Planet : MonoBehaviour
    8. {
    9.     //Variables from scaler
    10.     public float radiusDisplayed;
    11.     public float gravity;
    12.     public float dMass;
    13.  
    14.     //Variables relating to orbit
    15.     Rigidbody rb;
    16.     public Vector3 Velocity { get; private set; }
    17.     public Vector3 initialVelocity;
    18.     public string bodyName = "Unnamed";
    19.  
    20.     PlanetScaler scaler;
    21.     void Start()
    22.     {
    23.         scaler = GetComponentInChildren<PlanetScaler>();
    24.         dMass = scaler.mass;
    25.         gravity = scaler.surfaceGravity;
    26.         radiusDisplayed = scaler.radiusDisplayed;
    27.  
    28.         rb = GameObject.Find("Rigidbody").GetComponent<Rigidbody>();
    29.         rb.mass = scaler.mass;
    30.         Velocity = initialVelocity;
    31.     }
    32.  
    33.     private void Update()
    34.     {
    35.         gravity = scaler.surfaceGravity;
    36.         radiusDisplayed = scaler.radiusDisplayed;
    37.         //dMass = scaler.mass;
    38.         Vector2 local = transform.localScale;
    39.         transform.localScale = new Vector2(radiusDisplayed, radiusDisplayed);
    40.         Vector2 world = transform.lossyScale;
    41.     }
    42.  
    43.  
    44.     public void UpdateVelocity(Planet[] allBodies, float timeStep)
    45.     {
    46.         foreach (var otherBody in allBodies)
    47.         {
    48.             if (otherBody != this)
    49.             {
    50.                 float sqrDst = (otherBody.rb.position - rb.position).sqrMagnitude;
    51.                 Vector3 forceDir = (otherBody.rb.position - rb.position).normalized;
    52.  
    53.                 Vector3 acceleration = otherBody.scaler.mass * Universe.gravitationalConstant * forceDir / sqrDst;
    54.                 Velocity += acceleration * timeStep;
    55.             }
    56.         }
    57.     }
    58.  
    59.     public void UpdateVelocity(Vector3 acceleration, float timeStep)
    60.     {
    61.         Velocity += acceleration * timeStep;
    62.     }
    63.  
    64.     public void UpdatePosition(float timeStep)
    65.     {
    66.         rb.MovePosition(rb.position + Velocity * timeStep);
    67.     }
    68.  
    69.     private void OnValidate()
    70.     {
    71.         scaler.mass = scaler.surfaceGravity * scaler.radiusDisplayed * scaler.radiusDisplayed / Universe.gravitationalConstant;
    72.         gameObject.name = bodyName;
    73.     }
    74.  
    75.     public Rigidbody Rigidbody
    76.     {
    77.         get
    78.         {
    79.             return rb;
    80.         }
    81.     }
    82.  
    83.     public Vector3 Position
    84.     {
    85.         get
    86.         {
    87.             return rb.position;
    88.         }
    89.     }
    90. }
    and here is script B (PlanetScaler):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlanetScaler : MonoBehaviour
    6. {
    7.     //User-adjusted values
    8.     public double planetMass;
    9.     public double density;
    10.  
    11.     //Calculated values
    12.     public double volume;
    13.     private double radius;
    14.     public float radiusDisplayed;
    15.     public float mass;
    16.     public float surfaceGravity;
    17.  
    18.  
    19.     void Update()
    20.     {
    21.         //Calculations that determine the various parameters of the planet.
    22.         volume = planetMass / density;
    23.  
    24.         //radius = System.Math.Pow(3 * (volume / (4 * System.Math.PI)), 1 / 3);
    25.         radius = System.Math.Sqrt(volume / System.Math.PI);
    26.  
    27.         radiusDisplayed = (float)System.Math.Log(radius / 10000000000, 10);
    28.         //(float)(((System.Math.Log(radius, 25)) - 30) * 40);
    29.  
    30.         mass = (float)planetMass;
    31.     }
    32. }
    33.  
    I'm doing it this way because the way it's programmed the sun in my model needs to have the "Planet" script, so I'm going to include a section in the "PlanetScaler" script where it checks to see if the object it's parented to has a specific tag, and uses different calculations based on whether or not it's a planet or a sun.

    Also, the code governing where the body goes is not mine, I copied it from a video by Sebastian Lague (Coding Adventure - Solar System).

    Thanks!