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 Movement

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

C# fly movement error

This is my error : 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/TransformBindings.gen.cs:27) FlyMovement.Update () (at Assets/Scripts/FlyMovement.cs:19) This is my code : using System.Collections; using System.Collections.Generic; using UnityEngine;

public class FlyMovement : MonoBehaviour {

[SerializeField]
private Transform center;

private float flySpeed;

// Use this for initialization
void Start () {
    flySpeed = Random.Range (300f, 700f);
}

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

} Please help.

Mark VonGyer
Mark VonGyer
21,239 Points

Reading the exception message, you are attempting to use the Transform variable within the update function, however it has not been initialised or set. Haven't touched Unity for a while, but I think you've missed something with the Transform part

Tojo Alex
Tojo Alex
Courses Plus Student 13,331 Points

Thanks for the help but it is still not working

2 Answers

In the inspector/editor window for the FlyMovement component you need to drag the Fly gameobject to the transform "Center" in the inspector. At the moment it is empty which is why it is saying it's not referenced.

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

yes i know i already fixed the problem weeks ago though thanks anyway