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 trialBaye Dieng
Courses Plus Student 4,011 Pointsmy 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);
}
}
}
8 Answers
Sean May
9,145 PointsGot 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.
Baye Dieng
Courses Plus Student 4,011 PointsThanks 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
9,145 PointsLooking 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.
Baye Dieng
Courses Plus Student 4,011 Pointsthe field in my inspector is not showing up after i save
Sean May
9,145 PointsHmm, strange.
Would you possibly be able to take a screenshot of the Inspector when you have the GameManager selected?
Baye Dieng
Courses Plus Student 4,011 Pointshow do you post a picture on treehouse
Sean May
9,145 PointsYou'd need to upload it to a image hosting service like Imgur for example.
Then paste the URL
Baye Dieng
Courses Plus Student 4,011 PointsBaye Dieng
Courses Plus Student 4,011 Pointsthanks a lot
Baye Dieng
Courses Plus Student 4,011 PointsYou solved my code
Baye Dieng
Courses Plus Student 4,011 PointsHave a nice day
Sean May
9,145 PointsNo problem! Have fun with the rest of the course!
Sean May
9,145 PointsSean May
9,145 PointsThe 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?