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 Spawn Pickups Randomly

my code is perfect but the prefab box did not show up after i deleted the fly gameobject?

using UnityEngine; using System.Collections;

public class PlayerMovement : MonoBehaviour {

private Animator playerAnimator;
private float moveHorizontal;
private float moveVertical;
private Vector3 movement;
private float turningspeed = 20f;
private Rigidbody playerRigidbody;

// Use this for initialization
void Start () {
    playerAnimator = GetComponent<Animator> ();
 playerRigidbody = GetComponent<Rigidbody> ();


}


// Update is called once per frame
void Update () {
    moveHorizontal = Input.GetAxisRaw("Horizontal");
    moveVertical = Input.GetAxisRaw("Vertical");

    movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
}

void FixedUpdate() {
    if (movement != Vector3.zero) {

        Quaternion targetRotation = Quaternion.LookRotation(movement, Vector3.up);

        Quaternion newRotation = Quaternion.Lerp (playerRigidbody.rotation, targetRotation, turningspeed * Time.deltaTime);

        playerRigidbody.MoveRotation(newRotation);


        playerAnimator.SetFloat("Speed", 3f);
    } else {
        playerAnimator.SetFloat("Speed", 0f);
    }
}

}

Sean May
Sean May
9,145 Points

The code to set up the flyPrefab wouldn't be in the PlayerMovement script, it would be in the FlySpawner script. Can you post the FlySpawner script so we can take a look at it?

8 Answers

Sean May
Sean May
9,145 Points

Got it!

Your FlySpawner script isn't attached to the Game Manager object.

Go to your scripts folder, select the Game Manager, then drag the FlySpawner script into the Inspector.

Thanks for answering here is my FlySpawner script using UnityEngine; using System.Collections;

public class FlySpawner : MonoBehaviour {

[SerializeField]

private GameObject flyPrefab;
[SerializeField]
private int totalFlyMinimum = 12;

private float spawnArea = 25f;

public static int totalFlies;

// Use this for initialization
void Start () {
    totalFlies = 0;
}

// Update is called once per frame
void Update () {

    while (totalFlies < totalFlyMinimum) {
        totalFlies++;


        float positionx = Random.Range(-spawnArea, spawnArea);
        float positionz = Random.Range(-spawnArea, spawnArea);


        Vector3 flyPosition = new Vector3(positionx, 2f, positionz);

        Instantiate(flyPrefab, flyPosition, Quaternion.identity);

    }

}

}

Sean May
Sean May
9,145 Points

Looking at your script, I think I may need a little more clarification on what you mean by 'prefab box'. Do you mean the field that would be in the GameManager object that allows you to set the flyPrefab in the Inspector, like this?

if so, the

[SerializeField]
private gameObject flyPrefab

lines should be doing that. Are you not getting the field in the Inspector once you save the script?

You have an extra line break after the [SerializeField] line, but I tested doing that with my own code in Unity and it didn't change anything since C# doesn't care about whitespace.

the field in my inspector is not showing up after i save

Sean May
Sean May
9,145 Points

Hmm, strange.

Would you possibly be able to take a screenshot of the Inspector when you have the GameManager selected?

how do you post a picture on treehouse

Sean May
Sean May
9,145 Points

You'd need to upload it to a image hosting service like Imgur for example.

Then paste the URL

thanks a lot

You solved my code

Have a nice day

Sean May
Sean May
9,145 Points

No problem! Have fun with the rest of the course!