Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Objects dont instance

Discussion in '2D' started by roge530, Jan 19, 2020.

  1. roge530

    roge530

    Joined:
    Jan 1, 2020
    Posts:
    1
    Hello i have a little problem, im working on an Asteroid clone for my class but when i try to use methods from a prefab script it just not work, and its rare because i have another script in the same game with the same needs that just work fine

    Asteroid Spawner Code:

    Code (CSharp):
    1. Object prefabAsteroid;
    2.     GameObject[] numAsteroids;
    3.     Asteroid asteroidAux;
    4.     Vector3[] position;
    5.     Vector3 worldPosition;
    6.     int cont = 0;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         prefabAsteroid = Resources.Load(@"Prefabs\Asteroid", typeof(GameObject));
    11.         position = new Vector3[4];
    12.         position[0].x = Screen.width;
    13.         position[0].y = Screen.height / 2;
    14.  
    15.         position[1].x = Screen.width / 2;
    16.         position[1].y = 0;
    17.  
    18.         position[2].x = 0;
    19.         position[2].y = Screen.height / 2;
    20.  
    21.         position[3].x = Screen.width / 2;
    22.         position[3].y = Screen.height;
    23.  
    24.         for (int i = 0; i < 4; i++) position[i].z = -Camera.main.transform.position.z;
    25.     }
    26.  
    27.     void Positioner(int cont, float angle)
    28.     {
    29.         worldPosition = Camera.main.ScreenToWorldPoint(position[cont]);
    30.         GameObject asteroid = Instantiate(prefabAsteroid) as GameObject;
    31.         asteroid.transform.position = worldPosition;
    32.         asteroidAux = asteroid.GetComponent<Asteroid>();
    33.         asteroidAux.AngleSetter(angle);
    34.     }
    when Positioner its called, spawns an Asteroid just fine

    Ship code:
    Code (CSharp):
    1.  Object prefabMissile;
    2.     Missile missileAux;
    3.     const float waitInvervale = 1.33f;
    4.     Timer missileIntervale;
    5.     float currentTime = 0;
    6.  
    7.     Vector2 thrustDirection;
    8.     Rigidbody2D rb;
    9.     const float ThrustForce = 5;
    10.     const float RotateDegreesPerSecond = 90;
    11.  
    12.    
    13.     /// <summary>
    14.     /// User this for initialization
    15.     /// </summary>
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.  
    20.  
    21.         prefabMissile = Resources.Load(@"Prefabs\Missile", typeof(GameObject));
    22.         missileIntervale = gameObject.AddComponent<Timer>();
    23.         missileIntervale.Duration = waitInvervale;
    24.         missileIntervale.Run();
    25.     }
    26.  
    27.     /// <summary>
    28.     /// Update is called once per frame
    29.     /// </summary>
    30.     void Update()
    31.     {
    32.         float rotationInput = Input.GetAxis("Rotate");
    33.         float rotationAmount = RotateDegreesPerSecond * Time.deltaTime;
    34.         if (rotationInput > 0)
    35.         {
    36.             rotationAmount *= -1;
    37.             transform.Rotate(Vector3.forward, rotationAmount);
    38.         }
    39.         if (rotationInput < 0)
    40.         {
    41.             rotationAmount *= 1;
    42.             transform.Rotate(Vector3.forward, rotationAmount);
    43.         }
    44.        
    45.  
    46.     }
    47.  
    48.     void Shot()
    49.     {
    50.         GameObject missile = Instantiate(prefabMissile) as GameObject;
    51.         missile.transform.position = gameObject.transform.position;
    52.         missileAux = missile.GetComponent<Missile>();
    53.         missileAux.Shot(thrustDirection);
    54.     }
    In ship, i want to call the missiles and give them a direction to go, but every time I call them with missileAux, Unity throw me a warning saying

    NullReferenceException: Object reference not set to an instance of an object
    Missile.Shot (UnityEngine.Vector2 direction) (at Assets/Scripts/Missile.cs:36)
    Ship.Shot () (at Assets/Scripts/Ship.cs:63)
    Ship.FixedUpdate () (at Assets/Scripts/Ship.cs:88)

    What i can do to fix it?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    That Null Reference Exception is telling you that there is a null object in your "Missile" class in the method "Shot", so take a look at what might be null in there.