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.

Zombies Remaining results at the end of the game

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

  • Zombies Remaining results at the end of the game

    Hi there, I'm finishing up the Zombie Gallery. I'm nearly finished except for the part where once we reach the helicopter, I would either win or lose based on the zombies I have left. I used a gameobject "titled EndGame" with a box collider on Trigger along with a proper counter for how many zombies are left with each hit. However, I don't get the proper console alert for the end of my game once I reach EndGame. Here is my Code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;

    public class RCastPistolScript : MonoBehaviour
    {
    public GameObject bulletHole;
    public int ammoLimt;
    public Text ammo;
    public int zombiecount;
    public Text count;
    public GameObject endGame;

    void Update()
    {
    RaycastHit hit;

    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
    {
    Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
    }

    if(Input.GetButtonDown("Fire1"))
    {
    Instantiate(bulletHole, hit.point, Quaternion.LookRotation(hit.normal));

    if(hit.collider.gameObject.tag == "Zombie")
    {

    iTween.RotateTo(hit.collider.gameObject.transform. parent.gameObject, iTween.Hash(
    "x", 90f,
    "time", .5f,
    "easetype", "spring"
    ));
    zombiecount = zombiecount - 1;
    count.text = zombiecount.ToString();


    }

    if(hit.collider.gameObject.tag == "Child")
    {
    print("Oh no! A kid!");
    }

    ammoLimt = ammoLimt - 1;
    ammo.text = ammoLimt.ToString();
    }
    }
    private void OnTriggerEnter(Collider other)
    {
    if (endGame)
    {
    if (zombiecount <= 0)
    {
    print("You win!");
    }
    }

    if (zombiecount > 0)
    {
    print("You forgot one!");
    }


    }
    }

  • #2
    So your posted script is keeping track of your zombieCount; but you need to have your Trigger know what that count is when it gets triggered. So the script on the triggger needs to check with youRCastPistol script to find out if the zombie count = 0.

    So on the trigger, you'd have something like:
    Code:
    void OnTriggerEnter (collider other)
    {
         int zombieCount;
         zombieCount = GameObject.Find("ThingYouHaveTheRayCastingScriptOn").GetComponent<RCastPistolScript>().zombiecount;
    
         if (zombieCount <= 0)
         {
              //Fire up the YouWin end game level
         }
         else
         {
              //Fire up YouLose end game level
         }
    }

    Comment


    • #3
      Thank you for the help sir!

      Comment

      Working...
      X