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 Add Pickup Particles

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

C# error with the Fly Pickup

This is the error with Unity :

UnassignedReferenceException: The variable pickupPrefab of FlyPickup has not been assigned. You probably need to assign the pickupPrefab variable of the FlyPickup script in the inspector.

This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlyPickup : MonoBehaviour {

    [SerializeField]
    private GameObject pickupPrefab;


    void OnTriggerEnter(Collider other){
        // If the collider : other is tagged with "player"
        if (other.CompareTag ("Player")) {

            //...add the pickup particles ...
            Instantiate(pickupPrefab, transform.position, Quaternion.identity);


            Destroy(gameObject);


        }

    }
}

Please help.

2 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hi Tojo,

This error is caused by a missing assignment to your pickupPrefab at runtime. You'll need to drag the FlyParticles prefab from the Project window and drop it on the Pickup Prefab property of the Fly Pickup script in the inspector.

Watch the video starting at 4:45 where Nick performs this step.

I hope this helps.

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

thank you I all ready fixed the error this morning and it was based on your answer