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

Darius Dennis
Darius Dennis
1,433 Points

Once Frog is eaten sometimes bird keeps moving, frog disappears after 4-5 seconds. How to fix

I don't know whats wrong and why the bird keeps walking or why the frog just disappears after 4-5 seconds its doing it and I have been busting my butt trying to find out why please help!!

Darius Dennis
Darius Dennis
1,433 Points

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

public class BirdMovement : MonoBehaviour {

[SerializeField]
private Transform target;
private UnityEngine.AI.NavMeshAgent birdAgent;
private Animator birdAnimator;

// Use this for initialization
void Start () {

    birdAgent = GetComponent<UnityEngine.AI.NavMeshAgent> ();
    birdAnimator = GetComponent<Animator> ();

}

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

    birdAgent.SetDestination (target.position);

    // Measure the magnitude of the NewMeshAgent's velocity
    float speed = birdAgent.velocity.magnitude;

    //Pass the velocity to Animator component
    birdAnimator.SetFloat("Speed", speed);

}
Darius Dennis
Darius Dennis
1,433 Points

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

public class PlayerHealth : MonoBehaviour {

public bool alive;

[SerializeField]
private GameObject pickupPrefab;

// Use this for initialization
void Start () {

    alive = true;

}

void OnTriggerEnter(Collider other) {

    if (other.CompareTag ("Enemy") && alive == true) {

        alive = false;

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

    }

}

}

Darius Dennis
Darius Dennis
1,433 Points

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

public class PickupParticlesDestruction : MonoBehaviour {

// Use this for initialization
void Start () {

    //Destroy the "pcikup particles" after 5 seconds
    Destroy (gameObject, 5f);

}

}

Darius Dennis
Darius Dennis
1,433 Points

I'm trying to keep the frog on the screen until eaten

Darius Dennis
Darius Dennis
1,433 Points

The 5f is suppose to be to destroy the pick particles after 5 seconds

2 Answers

Ian Burres
Ian Burres
250 Points

I am not familiar with unity or C#, which is what I think you're using here, but what you've written reads

if (the object is an enemy and it is alive, then its state should be set to true). Else, the state of the object should be set to false. So why are you then assigning false to alive in your OnTriggerEvent? I think this might be where your error lies, but I can't compile your code to check it for myself right now.

Ian Burres
Ian Burres
250 Points

Also, you're destroying the gameObject after 5 seconds, which is probably why the frog is disappearing after that amount of time has expired. Are you trying to keep the frog on screen until it is eaten? Can you be a little more specific with what you're code is supposed to accomplish?

Ian Burres
Ian Burres
250 Points

Ok, that's what I figured. Correct me if I'm wrong then, but if the object is an Enemy, and the frog is alive, you want the frog object to be destroyed. Right? If that's the case, you need to add some more code. You need to assign the value of alive (in this case false) to the frog, and then define what happens to the frog when it's state is false (or dead). Otherwise, if the frog is in a living state, nothing should happen to it. As for the bird, it looks like you might need a for loop. How long do you want it to walk?

Darius Dennis
Darius Dennis
1,433 Points

until the frog collides with it