Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Pickups Script the Fly Pickup

aaron joseph
aaron joseph
1,294 Points

NullReferenceException:

NullReferenceException: Object reference not set to an instance of an object FlyMovement.Update () (at Assets/Scripts/FlyMovement.cs:24)

using UnityEngine; using System.Collections;

public class FlyMovement : MonoBehaviour { [SerializeField] private Transform center;

private float flySpeed;

// Use this for initialization
void Start()
{

    //flys will move at speed between two numbers
    flySpeed = Random.Range(300f, 700f);

}

// Update is called once per frame
void Update()
{
    //This will randomise the numbers from above
    transform.RotateAround(center.position, Vector3.up, flySpeed * Time.deltaTime);

}

}

1 Answer

Cam Newton
Cam Newton
1,737 Points

Check that your prefabs are hooked up in the Inspector panel. That's what it normally means for me, I forgot to drag the prefab into the script component so it knows what to spawn.