Need Help With Zombies

Ok so I need some help with my code. I want an animation to happen after I kill all the zombies (100) I know basic javascript. Here is my code:

 #pragma script
    
     var Zombie2 = 100;
    
     if (Zombie2 < 0) { GetComponent.().Play("Elevator"); }

Thanks :slight_smile:

Alright, well, you’re using GetComponent with an empty argument… I presume that you’re trying to gain access to an attached Animator component? Try this instead and let me know if it helps any (untested, top of my head)…

     #pragma script
      
         var Zombie2 = 100;
      
         if (Zombie2 < 0) { GetComponent.(Animator).Play("Elevator"); }

I have the following errors:

(5,21) Unexpected Token: …
(5,22) Insert semicolon at the end

GetComponent(Animator) not GetComponent.(Animator)

Ok so there are no errors, the animation is working, but the animation plays right away and is ignoring the if statement.
I want the animation to play after i kill 100 zombies (i changed it to 1 for testing purposes) Is it the script or do I have to change something in the Cube (animator property)?

All you’ve shown is a variable called Zombie2, and a condition that if that variable is BELOW zero, then an animation gets played.

What decrements this variable? Do you have something somewhere that says “when a zombie is killed, Zombie2 = Zombie2 - 1”?

Additionally, I’d expect this comparison to be inside an Update() loop.

My apologies, I’ve gotten used to C# and so Javascript now seems almost prehistoric. I love how unforgiving C# is because it forces you to declare your intentions properly, whereas JS is exceedingly forgiving & will allow you to declare variables without setting their type, etc. All that aside, as BenZed (love the avatar btw) said, where exactly are you decrementing the amount of zombies? Also, will there ever be less then 0 zombie2 at any point? Even when you have none, that’s 0… so how are you supposed to reach less than that?

Well I tried to put “Zombie2 = 0” but got an error…
I dont know a whole lot of coding all I’ve programmed before was programming buttons (start game, exit game, ect…)

Is it okay if you go more in depth im 12 and im trying to make a really cool game for everyone because im not very cool and I want to be a video game designer when I grow up so I want to start early…

First, don’t say you’re not cool. Confidence plays a huge role in how successful you will be in your life; practice being confident. It doesn’t matter what a few people might say about you. Some people are going to think you are pretty cool.

Second, it’s great that you are starting young while your brain still has the plasticity to develop a capacity for writing code and developing software. The older you are the harder it becomes to learn this stuff.

Third, learn C# if you want to be a game developer. Do not use Javascript just because it seems a little easier. C++/C# are pretty much an industry standard for object oriented programming and both of those languages are more applicable than Javascript in the game development field.

Lastly, your code snippet does not give enough information to solve your problem. As others before me have stated, we don’t know how the Zombie2 variable is decremented. What you need to do is write a script that subtracts 1 from the Zombie2 variable every time you eliminate a zombie. Then once Zombie2 reaches 0 your if statement should play the animation. That animation would be handled a little differently in C#

int Zombie2 = 100;
if(Zombie2 =< 0){
    gameObject.GetComponent<Animator>().Play("Elevator");
}

I advise you to go through these tutorials before you try to tackle a project of your own: https://unity3d.com/learn/tutorials/modules

Pace yourself; you are only 12 and you have lots of time to learn all of this stuff.

Good Luck!

3 Likes

I agree with everything @hamsterbytedev just said. I’ve been teaching programming to middle schoolers all semester, and I will say that you’re way ahead of the curve; your only misstep was in choosing JS over C#, but that’s easily fixed (do it now!).

I would add that “Need Help With Zombies” is the coolest thread title I’ve seen all day. It conjures up images of you barricaded behind an overturned table, desperately posting for help to the Unity forums while the zombies try to break down your door. Not a very informative subject, perhaps, but very evocative — it certainly caused me to click on it!

Cheers,

  • Joe
4 Likes

^ Me too. :slight_smile:

1 Like

You must be cool, kiddo! You’re already working on something that very few people have the willingness to learn :slight_smile: I’m almost 40 and am just getting into this stuff myself, so I can only imagine how awesome your games will be by the time you’re my age haha! Keep at it, and hit us up for help any ol’ time.

As Hamsterbyte,LLC asked, do you have anything in your code that subtracts from your Zombie2 count when a zombie is killed? If not, then the total will always be 100. When a zombie is destroyed, something should tell this script that Zombie2–;
or
Zombie2 = Zombie2 - 1;
so that your total goes down. Once you’ve got something like that in place, and you’ve adjusted your if statement as Hamsterbyte.LLC instructed, things should (hopefully) work out fine. If not, we’re here :slight_smile:

I was 11 when I started making games.

1 Like

Thanks man, you made my day :slight_smile:

1 Like

Ok question, to put the “Zombie2 - 1” would it be in the same, or different script.
Second can you maybe do a little example of what I should put for this^
All I know is " If zombie2(Clone) is destoryed (Zombie2 - 1), how would I put this into script “Form”…

I’m quite happy to work up a quick example for you, but you’ll have to give me a couple of hours. I’m in the middle of watching TV with my better half, and that’s quality time haha :slight_smile: Back soon!

Alright, I’m back. Now then, assuming that the code you’ve shown us so far is attached to your elevator, let’s make a couple of minor adjustments:

public int Zombie2 = 100; // amount of zombies to start with
public int ticks = 0; //a simple counter

void Update() // called once per frame
{
    if(Zombie2 =< 0 && ticks == 0) // if there are 0 or less zombies and our ticks counter is still at 0
    {
        ticks++; // add 1 to our ticks counter (this makes sure this will only happen once and not over and over every frame)
        gameObject.GetComponent<Animator>().Play("Elevator"); // get access to the Animator component of this object & play the "Elevator" animation
        Debug.Log(gameObject.name + " says there are no zombies left! Playing the Elevator animation now!"); // write a message to the console so we can see when it happens
    }
} // end of Update() function

public void ZombieDied() // we'll be calling this from whatever script is attached to the Zombies
{
   Zombie2--; // subtract 1 from whatever number Zombie2 is currently
   Debug.Log(gameObject.name + "  reports that the number of zombies is now " + Zombie2); // write a message to the console so that we know how many zombies there are
} // end of ZombieDied() function

Now that we have the elevator code in place, let’s make the zombies tell the elevator when they die… Something like this next piece of code should be placed on your zombies, in whatever function you’re using to Destroy them. Just as an example:

public int health = 100; // whatever health the zombie has
public GameObject elevator; // the Elevator gameobject in your scene (drag and drop in the Inspector)

void Update()
{
   if(health <= 0) // when the zombie's health is 0 or less,
   {
        elevator.BroadcastMessage("ZombieDied"); // tell the elevator script to subtract 1 zombie
        Debug.Log(gameObject.name + " is dead. Subtracting one from Elevator's Zombie2 counter."); // write a line to the console so that we know when this is happening
        Destroy(gameObject); // and destroy the zombie
   }
} // end Update() function

Again, this is just a rough example of what you might do. There are a lot of ways to approach this, and several ways of making scripts talk to each other. I’m using this method because I’m showing you C# and you’re already using JavaScript. The two don’t always get along together, but the BroadcastMessage call should be able to bridge the gap between them just fine. I’ve added Debug lines so that you can tell when things are happening by watching your Console as well. They’re not necessary, but may help :slight_smile: Let me know how it goes!

1 Like

Okay I can’t try it right away, but I will make script files for these, but I will have to do it later today (grandmaws lol)

1 Like

No worries, just let me know how it works out when you get a chance to test it and we’ll take it from there. Have fun!