• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by adrennalinpro · May 15, 2013 at 04:51 AM · randomdeathmessagediehealth

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 w$$anonymous$$ch 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 (w$$anonymous$$ch 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 somet$$anonymous$$ng 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.

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · May 15, 2013 at 05:00 AM 0
Share

For a random message, look into array's and Random.Range().

 var myMessages : String[];  // Initialize in the inspector

Then to get the message:

 if (myMessages.Length > 0)
   Debug.Log(myMessage(Random.Range(0,myMessages.Length));


1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adrennalinpro · Apr 14, 2014 at 05:32 AM 0
Share

Dude you're a genius....

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scene not restarting after player death 2 Answers

Player Health Damage 2 Answers

How To Make Health Decrease 4 Answers

How to Load Main Menu on Player's Death 2 Answers

I have an attack/take damage script how do I make the player die when health hits 0? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges