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

Fly not being "destroyed"; (potentially related) "UnassignedReferenceException" error

Hello! Thanks for this great series. I really appreciate you taking the time to put together a great template program to build from start-to-finish.

In short, my fly isn't being "destroyed" upon contact with my frog. I have watched and reviewed your FlyMovement and FlyPickup videos, but I don't seem to have overlooked anything. I speculate that the following compiler error is related, but cannot confirm whether this is the case.

The full text of the error is: UnassignedReferenceException: The variable center of FlyMovement has not been assigned. You probably need to assign the center variable of the FlyMovement script in the inspector. UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineTransformBindings.gen.cs:26) FlyMovement.Update () (at Assets/Scripts/FlyMovement.cs:20)

Have I missed something? Have I somehow failed to "assigned the center variable of the FlyMovement script in the inspector"? I have pasted my FlyMovement.cs text below.

Thanks, in advance, for any help or insight that you can provide!

using UnityEngine;
using System.Collections;

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

    private float flySpeed;

    // Use this for initialization
    void Start () {
        //the following "range" method picks a value between the minimum float (300) and the maximum float (700)
        flySpeed = Random.Range (300f, 700f);


    }

    // Update is called once per frame
    void Update () {
        transform.RotateAround (center.position, Vector3.up, flySpeed * Time.deltaTime);



    }
}

Eesch, sorry everyone. This didn't paste as neatly as I had anticipated.

fixed code formatting

1 Answer

Hi Andrew,

Your problem might be that you didn't add the Fly transform to your Center field in the Inspector panel for your Geometry object.

Nick does this in the previous video Script the Fly Movement

This starts at around 4:25 in that video.

You want to make sure that with your geometry object selected in the hierarchy, you drag the parent fly object to the center field in the inspector panel.