Search Unity

Need Help With Zombies

Discussion in 'Scripting' started by Willy_2202, Apr 29, 2015.

  1. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Before I try it, what do I need to change to make it to where I only have to kill one...

    1.Where
    2.Which Script
     
  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    I'm going to go ahead and tell you, however I'm a bit disappointed because it sounds like you're not really paying any attention to the contents of these scripts at all. I'm here to help you learn, not just to do the work for you. For that reason, I'm going to make you wait and think about it a little bit. If you look over the two scripts we've been working on for the past couple of days, you should be able to find the answer yourself with no trouble at all. If not, I'll be back in a couple of hours to spell it out. I'm not trying to be hard on you, but I expect you to try and actually gain some knowledge here.
     
  3. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    I understand I guess you have been doing alot of the work Im sorry
     
  4. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Ok its the Elevator Script:

    Code (CSharp):
    1. if(zombiesKilled == 1 && ticks == 0) // if 100 zombies have been killed and our ticks counter is at 0
    2.         {
    3.             ticks++; // add one to our ticks counter so that this code is only processed once
    4.             anim.Play("Elevator"); // get access to the Animator component of this object & play the "Elevator" animation
    5.             Debug.Log(gameObject.name + " says 100 zombies have been killed! Playing the Elevator animation now!"); // write a message to the console so we can see when it happens
    6.         }
    7.     } // end of Update() function
    This isn't the whole code just the part..
    but I killed one zombie and nothing...
    But I have an idea.
    Ok so you told me to make a new state in the animator.
    Do i have to make one of those arrows or edit any thing or is it just not working
     
  5. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Woohoo! You found it :) Awesome! Great job!

    The new state we made in the Animator was just created so that the other animation, "Elevator", wouldn't play automatically. It shouldn't require any transition settings. At this point, I can't even guess as to why it isn't playing.

    Take a screenshot of your Animator states and post them for me, please, and perhaps that will give me an indication as to what else might be going wrong. I'm pretty frustrated with all of this, because the entire ordeal should have been an incredibly simple matter to fix, and I'm left to once again assume that we're just missing some tiny, but vital, bit of information about how you have things set up.
     
    Stef_Morojna likes this.
  6. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Same here....
    Ok heres a picture:

    ElevatorAnimation.png

    This is the Elevator by the way, (Cube).
     
  7. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    I am also >Trying> to learn c# and HTML and so far I've made just a little simple browser. If you want to check it out.Made with Visual Studio c#

    https://sites.google.com/site/dylansflashgames/

    ------------------------------------------------------------------------------------------------------------------------------------------------------------
    To the point.
    I am confused too, but its always that one little thing right haha.
     
  8. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Awesome, I'll certainly have a look when I have a few free minutes :)
    As for the Animator, everything appears to be in order... *sigh* Alright, let's take this back to basics...
    1) Kill a zombie.
    2) Watch the Elevator's Animator window and see if the "Elevator" animation tries to play.
    3) Check your Console and see if you have the following lines:
    • "Zombie (or whatever you have it named) is dead. Adding one to the Elevator's zombiesKilled counter."
    • "Elevator says 100 zombies have been killed! Playing the Elevator animation now!"
    You should be seeing both messages, and of course animation should be playing... Let me know and we'll try again then.
     
  9. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Ok so I tried, NOW I have "Zombie2(Clone) is dead. Addingone to Elevator's zombiesKilled counter, but then I have an Error (Not the error that doesn't let you play but a broadcast error thing) "BroadcastMessage ZombieDied has no receiver" , though the elevator script is attached to the elevator, I guess its the elevator not receving it but not 100% sure. <---- Animation doesn't play because of this I think so if we can fix this then we will be able to tell.

    I won't answer or test for about 8hrs Im in middle school so I will try it when I get back if you have anything....
     
  10. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    *sigh* *facepalm* *headdesk*

    Alright... Paste up your current ElevatorScript code... I just ran a test to make sure I wasn't missing anything on the communication side, and BroadcastMessage works fine for talking between JS and C#, so that's not it... I can see from the changes we made to your Zombie script that everything is as it should be, so long as the ElevatorScript is set up correctly... Of course, if it's just like the one I posted the last time, then even that shouldn't be a problem because it looks fine too. We could throw in a DontRequireReceiver line or something, but that's not going to fix the problem, just stop the error, so that's pointless. *maniacal laughter* This REALLY shouldn't be this complicated, hahahahahahaha.....
     
    Last edited: May 5, 2015
  11. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    @krougeau, your patience & persistence in helping this yoot is an inspiration. Please keep it up — and if you do reach the end of your rope, and want to hand it off, let me know and I'll have a go. (Though I'd probably start by having him ditch JS completely and switch everything to C#...)

    EDIT: Just realized that some readers may be too young to know what a "yoot" is, and think it's a bad term. It's not; it's an affectionate term for "youth," originally from the movie "My Cousin Vinny." :)
     
    hamsterbytedev likes this.
  12. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Thanks Joe. LOL! Yeah, I could totally hear the Jersey / Brooklyn in it ;) "Whachoos lookin' at?" :p

    As for the "patience", it's probably more sheer tenacity, haha. I'm loathe to give up on things. Steadily tilting at windmills :) I've been thrilled to discover that I've actually gotten my head around enough of the basics to be of help to others, and am glad to pay forward the assistance I've received from a variety of places. It's also a nice way to sharpen my skills. By helping troubleshoot other people's issues, I'm encountering problems I might never have had to solve on my own, so it's a win-win!
     
    Last edited: May 5, 2015
    JoeStrout likes this.
  13. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Yes you are doing a great job helping, and I am trying to learn c# and use it a little bit more ;)
    Ok here is ElevatorScript.cs:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ElevatorScript : MonoBehaviour
    5. {
    6.     private int zombiesKilled = 0; // count of how many zombies have been killed by the player
    7.     private int ticks = 0; // a simple counter
    8.     private Animator anim; // the Animator component attached to this object
    9.    
    10.     void Start()
    11.     {
    12.         anim = GetComponent<Animator>(); // get access to the Animator attached to this object
    13.     }
    14.    
    15.     void Update() // called once per frame
    16.     {
    17.         if(zombiesKilled == 1 && ticks == 0) // if 100 zombies have been killed and our ticks counter is at 0
    18.         {
    19.             ticks++; // add one to our ticks counter so that this code is only processed once
    20.             anim.Play("Elevator"); // get access to the Animator component of this object & play the "Elevator" animation
    21.             Debug.Log(gameObject.name + " says 100 zombies have been killed! Playing the Elevator animation now!"); // write a message to the console so we can see when it happens
    22.         }
    23.     } // end of Update() function
    24.    
    25.     public void ZombieDied() // we'll be calling this from whatever script is attached to the Zombies
    26.     {
    27.         zombiesKilled++; // add 1 to zombiesKilled
    28.         Debug.Log(gameObject.name + "  reports that the player has killed " + zombiesKilled + " zombies!"); // write a message to the console so that we know how many zombies there are
    29.     }
    30. }
    To let you know when I attached it to the elevator, there is no space for variables, gameobjects or anything like that like the ZombieDamage script, im letting you know this just in case that may or may not be a problem....
     
  14. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Hmmm... I'll have to test it, but it may be because we made zombiesKilled a private variable... I only did that because you're not assigning it yourself, but having that number set from information passed from the other script. All the same, change
    private int zombiesKilled = 0;
    to
    public int zombiesKilled = 0;
    and give it a shot. I'd test it for you, but I'm caught up in other things yet again at the moment & won't be free until late this evening, when I'm actually hoping to catch up on some sleep haha ;) Let me know if it makes a difference!
     
  15. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Same error and when I look at the script when its attached to the elevator, the Zombies Killed Variable isn't moving...
    Though in game if I change it myself the animation plays, so we just need it to change automatically...

    Error: BroadcastMessage ZombieDied has no receiver
     
  16. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    OK, I've tested SendMessage and BroadcastMessage both on my side & have had no trouble getting the function in my C# script to fire when called from the Javascript file. Changing the int from public to private didn't affect it any either. I'm left to assume, once again, that there's something we're missing in your setup...

    On your Zombie script, you've dragged and dropped the Cube elevator object from your Heirarchy into the "elevator" slot of the script, correct? Not from a folder in your Assets, and not the script attached to the Elevator, but the Elevator game object that's in your scene, correct?

    Other than checking to be sure that the above is done correctly, double check that the ElevatorScript is attached properly to the Elevator gameobject, not it's parent or child (although it should work fine on child objects as well, but just to be sure).

    Beyond that, I'm at a loss.
     
  17. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Here's another quick test for you... Make a couple of objects in your scene (I used a cube and a sphere, but almost anything should be fine), then attach the following JS script to one of them and the CS script to the other.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var other : GameObject;
    4.  
    5. function Update ()
    6. {
    7.     if(Input.GetKeyDown("space"))
    8.     {
    9.         Debug.Log(gameObject.name + " is telling " + other.gameObject.name + " to do stuff...");
    10.         // other.SendMessage("doStuff");
    11.         other.BroadcastMessage("doStuff");
    12.     }
    13. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestCS : MonoBehaviour {
    5.    
    6.     private int testNumber = 0;
    7.    
    8.     public void doStuff()
    9.     {
    10.         testNumber++;
    11.         Debug.Log(gameObject.name + " accessed! Test Number is now " + testNumber + ".");
    12.     }
    13. }
    Drag and drop the Object that you put the CS script on into the "Other" slot of the object that has the JS script on it. Once that's done, hit Play and then press Space when the game is running... In your console, you should see the following (in my tests, the JS script is on the cube and the CS script is on the Spehere):

    Cube is telling Sphere to do stuff...
    UnityEngine.Debug:Log(Object)
    Sphere accessed! Test Number is now 1.
    UnityEngine.Debug:Log(Object)

    If you press space again, you should see

    Cube is telling Sphere to do stuff...
    UnityEngine.Debug:Log(Object)
    Sphere accessed! Test Number is now 2.
    UnityEngine.Debug:Log(Object)

    and the test number should keep counting up every time you hit the space bar...
     
  18. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Ok I was selecting elevator prefab from assets, but when I try to put the one from the heirarchy it won't let me no error, just I can't.
     
  19. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Jeez mon, this is enough to make a grown man cry...

    OK, let's approach this from another angle... Drag your prefab zombie into the scene, then drag/drop the elevator from the scene into that zombie's "Elevator" slot and see if it will allow you to do that.

    I'm not sure what it's problem is, as we're just assinging a GameObject, but still, let's give this a go... IF it let's you do that, then update your prefab to be the zombie you have in your scene (select the zombie in the scene and hit Apply in the upper right of the inspector settings). This should update the prefab to be a copy of the zombie in the scene. Now you can delete the zombie from the scene, and try it out again...

    Granted, if it's still not letting you assign the Elevator to the slot, then we've got some other weird issue and we'll have to step back and attack the whole mess again in yet another new and inventive way, haha.
     
  20. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Ok yes it worked and the animation is now playing!! But when I hit apply the cube won't stay selected as elevator...
     
  21. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Cool deal, at least we're getting somewhere :) Try making the Elevator a prefab, then drag/drop that into your Zombie prefab. Let me know how it goes! I'll be back on later this evening to check in.
     
    Stef_Morojna likes this.
  22. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    We are back to the zombies killed not moving again lol uuuhhh
     
  23. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    LOL! Alright, well, even that's a bit of a clue to work with... Try it this way and see if we get any better results, or if it's the same as before...
    1. Drag the prefab zombie into the scene.
    2. Drag & drop the elevator from your scene into the zombie in your scene's "Elevator" slot.
    3. Drag and drop the zombie from your scene on top of the prefab zombie in your assets folder, which updates the prefab.
    4. Delete the zombie from your scene.
    5. Fire up the game and see what happens this time...
     
    Stef_Morojna likes this.
  24. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Ok so I did those Instructions and it has Error: The variable elevator of ZombieDamage has not been assigned.

    So I clicked on the prefab and it deselected cube.

    (Same as before)
     
  25. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Cool cool. I had a feeling the results likely would be, but at this point I'm just throwing everything at the wall to see what'll stick... Going to do some fiddling and further testing on my end before I make any further suggestions. I'll let you know if I get anywhere.
     
    Stef_Morojna likes this.
  26. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    ok
     
  27. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Found out anything?
     
  28. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Heya. Unfortunately, I've been tied up the past couple of days & haven't had time for much of anything. I'll let you know as soon as I can though.
     
    Stef_Morojna and Willy_2202 like this.
  29. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Your patience is infinite, Good on ya!
     
    Stef_Morojna likes this.
  30. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Ok
     
  31. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Alright, sorry to keep you waiting. I've been having "one of those days" for the past 6 weeks and things are weighing heavier than I'd like. In any case, I think I've got a simple solution for you at last, so try this...

    Change the Start function at the top of your ZombieDamage script, to look like the one below:

    Code (JavaScript):
    1. var maximumHitPoints = 100.0;
    2. var hitPoints = 100.0;
    3. var deadReplacement : Rigidbody;
    4. var GOPos : GameObject;
    5. var scoreManager : ScoreManager;
    6. var elevator : GameObject;
    7.  
    8. function Start()
    9. {
    10.     scoreManager = gameObject.Find("ScoreManager").GetComponent("ScoreManager");
    11.     elevator = GameObject.Find("Elevator"); // this will find the first object in the scene named Elevator
    12. }
    13.  
    14. function ApplyDamage (damage : float)
    15. {
    16.     if (hitPoints <= 0.0)
    17.         return;
    18.     // Apply damage
    19.     hitPoints -= damage;
    20.     scoreManager.DrawCrosshair();
    21.     // Are we dead?
    22.     if (hitPoints <= 0.0)
    23.         Replace();
    24. }
    25.  
    26. function Replace()
    27. {
    28.     // If we have a dead barrel then replace ourselves with it!
    29.     if (deadReplacement) {
    30.         var dead : Rigidbody = Instantiate(deadReplacement, GOPos.transform.position, GOPos.transform.rotation);
    31.         scoreManager.addScore(20);
    32.         // For better effect we assign the same velocity to the exploded barrel
    33.         dead.GetComponent.<Rigidbody>().velocity = GetComponent.<Rigidbody>().velocity;
    34.         dead.angularVelocity = GetComponent.<Rigidbody>().angularVelocity;
    35.     }
    36.     elevator.BroadcastMessage("ZombieDied"); // tell the elevator script to add 1 to the number of zombies killed
    37.     Debug.Log(gameObject.name + " is dead. Adding one to the Elevator's zombiesKilled counter."); // write a line to the console so that we know when this is happening
    38.     // Destroy ourselves
    39.     Destroy(gameObject);
    40. }

    What we've done is simply added one line of code to the Start() function that will look for a game object in your scene named Elevator, that way you don't have to assign it yourself, the zombies will just find it when they spawn. This SHOULD work :) At least my tests were succesfull with it, so hopefully yours will be as well. Best of luck!
     
  32. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    And, if you're curious, here's what I'm working on at the moment... It's terrible, lol, but much better than it used to be. Two steps forward, one step back kind of progress, but that's progress all the same...

     
  33. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Thats pretty cool for alpha, good job..
    Heres my website for 2 games I made (One with stencyl the other with unity)that zombie game that we are working on (it might help with our problem not sure):

    https://sites.google.com/site/dylansflashgames/
     
    krougeau likes this.
  34. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Thanks! Still a ton of work to do on it, as time allows.

    I've still got a tab open with your website, just haven't had time to download and check anything out yet :) As soon as I can though, I most certainly will. I've been fiddling around in Unity for three years or so now and still don't have a single "finished" project, lol, so you're already ahead of me haha :p Have fun & let me know if the new code (see response before last, post #81) works to fix the issue in question.
     
    Last edited: May 9, 2015
  35. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    The script didn't work, when I click apply or drag the zombie over the prefab it deselects "Cube" (elevator)

    Let me rename the cube to Elevator and see hold on
     
  36. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    YES!!! It works thank you so so much...
    Now just time to actually find a elevator model or something and then make the next level, thanks and if you ever want to help again or anything just go to my profile and see if I have any new threads thanks, reply so I know you have read this so I can close this...
     
    krougeau likes this.
  37. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Yeah, you don't need to assign the elevator anymore, just make sure it's called Elevator and it should work on it's own.
     
  38. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    HURRAY!!! *happydance!* At long last, a solution :p Glad to be of help. Have fun & if you need me for anything else, just hit me up. Have fun!
     
  39. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    1 more thing is there a way to make unity switch scenes after an animation plays?
     
  40. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
  41. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    "Closing Thread"