- Home /
How to make death messages show at random?
I have a deathScript and need to know how to create different messages show at random once dead. Right now they go to console which is fine for now. What I need is a bit of script that will cause different messages to appear. My current code just does one but as I've said there needs to be more messages and done random. my current script for sending the message is:
if(health <= 0)
{
Print("DEATHMESSAGEHERE")
Destroy(Player);
}
I need up to ten different messages, and display a random one each time you die. My theory is that you could make the damage of the enemy a random number between 1-10. Then I would make the health = 11 by default and if the equality is 1-10 then show a message for that designated number. The only problem is, I cannot figure out how to implement the math into the JavaScript (which is indeed what I am using). I would show the formula I am using for it but it's on another computer. I believe it is something to the effect of:
Math.random()
and I would have a long stream of code to tell it what to randomize and what the actual perimeters are.
EVEN IF YOUR SOLUTION IS TOTALLY UNRELATED TO MY THEORY PLEASE POST TO CORRECT/INFORM ME. I need to know so that I can 1. have random death messages and 2. have random death animations and 3. have random awesomeness.
For a random message, look into array's and Random.Range().
var my$$anonymous$$essages : String[]; // Initialize in the inspector
Then to get the message:
if (my$$anonymous$$essages.Length > 0)
Debug.Log(my$$anonymous$$essage(Random.Range(0,my$$anonymous$$essages.Length));
Answer by ikriz · May 15, 2013 at 06:37 AM
Create a Death Class with animation, message and awesomness.
public class Death {
string message;
AnimationClip animation;
int dramalevel;
public void Die()
{
// do stuff when player dies
}
}
Then for each type of death add it to a list
List<Death> deaths = new List<Death>();
Death tmp = new Death();
tmp.message = "OMG i died..";
tmp.animation = myanimation;
tmp.dramalevel = 1000;
deaths.Add(tmp);
and then when death occurs:
int selectedDeath = Random.Range(0,deaths.Count-1);
deaths[selectedDeath].Die();
and there you have it.
Your answer
Follow this Question
Related Questions
Player Health Damage 2 Answers
Scene not restarting after player death 2 Answers
Player taking damage on collision. Can't get the script to work!? 1 Answer
Creating a Asteroid health script 5 Answers
Player Character Health 2 Answers