Welcome!

Welcome to our community forums, full of great people, ideas and excitement. Please register if you would like to take part.

This is extra text with a test link..

Register Now

Announcement

Collapse
No announcement yet.

if Health = 0 activate Pick Up

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • if Health = 0 activate Pick Up

    I'm working on my project, and having issues getting my character to Only pick up the Object if it is Dead, Health = 0. I got a separate Health script & Pick Up Trigger script. I tried to combine them but I cant get them to work on only picking up when Dead, my script just picks up. I also have a bullet script that damages the Object"dog". this is just a mock up of my game to function properly.
    Code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PickUpScript : MonoBehaviour
    {
    public GameObject realDog;
    public GameObject fakeDog;
    public GameObject Chicodog;
    
    
    
    public void Start()
    {
    fakeDog.SetActive(false);
    Chicodog.SetActive(false);
    }
    
    void OnTriggerEnter(Collider other)
    {
    
    //if (GetComponent<DogHealth>().Health <= 0)
    {
    if (other.gameObject.tag == "Player")
    {
    
    realDog.SetActive(false);
    fakeDog.SetActive(true);
    Chicodog.SetActive(false);
    }
    
    }
    
    
    
    }
    }

  • #2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class DogHealth : MonoBehaviour
    {
    public int Health;






    public void DoDamage(int sentValue)
    {

    Health = Health - sentValue;

    if (Health <= 0)
    {

    //Destroy(gameObject);
    }

    }
    }[/CODE]

    Comment


    • #3

      Comment


      • #4
        A mockup like this is a good idea. It's a pretty standard way to work out the mechanics of a game.

        So there could be a couple of things that go badly here. Some things to check:

        1) PickupScript and DogHealth are both attached to the same Dog GameObject.
        2) You've got the line that actually checks for health commented out. Be sure you remove the "//" on this line: //if (GetComponent<DogHealth>().Health <= 0); or it won't work.
        3) The player is actually tagged as "Player"

        In other questions, so when the player runs over one of these "dead" dogs, then fakeDog is turned on, but a bunch of others are turned off? Where are these objects?

        Comment


        • #5
          I have the PickUp Script on a separate box trigger over the dog"box" I made it a child of the dog/box. The Dog Box has the health script. I took off the /// on the. if (GetComponent<DogHealth>().Health <= 0). this was my issue and still persist not reading his health when reaches zero. I tried to combine them but i need one to be a trigger & one to have collisions so I did it that way. The objects enabled would be some other dog breed.

          Comment


          • #6

            Comment


            • #7

              Comment


              • #8
                So take a look at the console in that first screenshot. Once you get a red error on runtime, you can't trust anything else that happens (the script give up). It looks like PickUp script can't find something you're telling it to find. Fix that error, and then see what happens.

                Comment

                Working...
                X