-1

So making an asteroid game, trying to spawn in 4 asteroids, one in each centre section of each side. Code is below for the spawnerScript & the Initialize method.

Got a couple of errors/Warnings:

NullReferenceException: Object reference not set to an instance of an object (UnityEditor.PropertyHandler.isArrayReordable

Should not be capturing when there is a hotcontrol

If someone could point out WHY it's giving that error would be appreciated

[SerializeField] GameObject prefabAsteroid;
List<GameObject> asteroids = new List<GameObject>();
List<Vector3> screenSides = new List<Vector3>();
// Start is called before the first frame update
void Start()
{
    //get screen numbers
    float screenHeight = ScreenUtils.ScreenTop - ScreenUtils.ScreenBottom;
    float screenWidth = ScreenUtils.ScreenRight - ScreenUtils.ScreenLeft;

    //collect all screen sides
    screenSides.Add(new Vector2(ScreenUtils.ScreenLeft,screenHeight/2));
    screenSides.Add(new Vector2(ScreenUtils.ScreenTop, screenWidth / 2));
    screenSides.Add(new Vector2(ScreenUtils.ScreenRight, screenHeight / 2));
    screenSides.Add(new Vector2(ScreenUtils.ScreenBottom, screenWidth / 2));
    //loop through each direction(Up,Left,Down,Right) and
    //Instantiate asteroid on center of each side.
    int i = 0;
    foreach (Direction dir in System.Enum.GetValues(typeof(Direction)))
    {

        GameObject pa = Instantiate<GameObject>(prefabAsteroid);
        //add to list
        asteroids.Add(pa);
        pa.GetComponent<Asteroid>().Initialize(dir, screenSides[i]);
        i++;
    }

public void Initialize(Direction direction, Vector2 location)
{
    transform.position = location;
    float angle = Random.Range(0 * Mathf.Deg2Rad, 30 * Mathf.Deg2Rad);
    Vector2 forceMagnitude = new Vector2(Random.Range(minImpulse, maxImpulse), Random.Range(minImpulse, maxImpulse));
    if(direction == Direction.Right)
    {
        angle +=-15*Mathf.Deg2Rad;
    }
    else if (direction == Direction.Left)
    {
        angle +=165 * Mathf.Deg2Rad;
    }
    else if (direction == Direction.Up)
    {
        angle += 75 * Mathf.Deg2Rad;
    }
    else if (direction == Direction.Down)
    {
        angle += 255 * Mathf.Deg2Rad;
    }
    Vector2 moveDirection = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
    rb.AddForce(forceMagnitude * moveDirection, ForceMode2D.Impulse);
}

Tried storing instantiated object into list, tried grabbing component from that but still same error

burnsi
  • 6,194
  • 13
  • 17
  • 27
Zachary.S
  • 17
  • 3

1 Answers1

1

Fixed it.

Error was the rigidbody component 'rb' wasn't within the Initialise (was in Start() method but that hasn't run at this point) so was considered null. Just noticed Ann.L saying same thing

Zachary.S
  • 17
  • 3