Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by EliteHedgehog56 · Jan 16, 2018 at 08:11 AM · variablesfloatspawningdisableenable

random respawn and respawn delay

so I have this health script that is attached to each player which when the player's health is depleted the player instantly respawns to assigned spawnpoint. what I want to do is temporarily disable to player when he/she is killed, wait 3 seconds then spawn at random spawnpoint and re enable at the same time, also I have set it to add 100 health back to the health float variable when respawned and I want it to set the float to 100 instead of adding to it.

how would I go adding these things into my script?

 #pragma strict
 
 var PointCube : Transform;
 var spawneffect : Transform;
 var deatheffect : Transform;
 var SpawnPoint : Transform;
  var respawn : boolean = false;
 var health:float = 90;
 var healthText : UI.Text;
 
  function Damage(dmg:float){
  health -= dmg;
  }
  
  function Update (){
  if(healthText)healthText.text = health.ToString();
  if(health <=0){
  //temporarily disable player
    Instantiate (deatheffect,transform.position,transform.rotation);
    Instantiate (PointCube,transform.position,transform.rotation);
    //wait 3 seconds
    //change this to respawn randomly
   transform.position = SpawnPoint.position;
     //enable player
 
     Instantiate (spawneffect,transform.position,transform.rotation);
 
   var audio: AudioSource = GetComponent.<AudioSource>();
     audio.Play();
     audio.Play(44100);
 
     //change this to set float not add to it
   health += 100;
  }
  
  }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by KittenSnipes · Jan 16, 2018 at 09:19 AM

@EliteHedgehog56

I am not absolutely fluent in the UnityScript but here is my best shot:

 #pragma strict
 
 var PointCube: Transform;
 var spawneffect: Transform;
 var deatheffect: Transform;
 var SpawnPoints: Transform[];
 private var SpawnPoint: Transform;
 
 //Respawn timer for the coroutine
 var respawnTime: float = 3;
 
 var respawn: boolean = false;
 
 //This is the players current health
 private var health: float;
 
 //The max health the player is spawned with
 var maxHealth: float = 90;
 
 var healthText: UI.Text;
 
 //At start set the players health to maxHealth
 function Start() {
     health = maxHealth;
 }
 
 function Damage(dmg: float) {
     health -= dmg;
 }
 
 function Update() {
     if (healthText) {
         healthText.text = health.ToString();
             // If the players health is zero start the respawn function
         if (health <= 0) {
             StartCoroutine(Respawn());
         }
     }
 }
 
 //This is a coroutine. Basically meaning it has a special wait timer function
 function Respawn() {
     //temporarily disable player
     Instantiate(deatheffect, transform.position, transform.rotation);
     Instantiate(PointCube, transform.position, transform.rotation);
 
     //Wait for the respawnTimer. (This is the special wait timer I was speaking of)
     yield WaitForSeconds(respawnTime);
 
     //Find a random spawn point
     var spawnPointID: float = Random.Range(0, SpawnPoints.length);
 
     //Set the player at that random spawn point
     transform.position = SpawnPoints[spawnPointID].position;
 
     //enable player
     Instantiate(spawneffect, transform.position, transform.rotation);
 
     var audio: AudioSource = GetComponent.<AudioSource>();
     audio.Play();
     audio.Play(44100);
 
     //change this to set float not add to it
     health = maxHealth;
 }
Comment
Add comment · Show 15 · 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 EliteHedgehog56 · Jan 16, 2018 at 12:00 PM 0
Share

@$$anonymous$$ittenSnipes this looks about right I'll give it a go and see if it works :)

avatar image KittenSnipes · Jan 16, 2018 at 12:03 PM 0
Share

@EliteHedgehog56

Hopefully it works :D

avatar image EliteHedgehog56 · Jan 16, 2018 at 12:08 PM 0
Share

@$$anonymous$$ittenSnipes

there is as small error with the respawn coroutine

alt text

capture.jpg (19.8 kB)
avatar image KittenSnipes · Jan 16, 2018 at 12:14 PM 0
Share

@EliteHedgehog56

Change this:

 StartCoroutine(Respawn);

To this:

 yield Respawn();
avatar image EliteHedgehog56 KittenSnipes · Jan 16, 2018 at 12:16 PM 0
Share

aah I see must of mixed it up with C# or somethin.

I'll give it a try again and hopefully it works :P

avatar image EliteHedgehog56 EliteHedgehog56 · Jan 16, 2018 at 12:19 PM 0
Share

@$$anonymous$$ittenSnipes

looks like it worked, I'm getting an error (which I've had for a while) but it's nothing drastic and it doesn't stop me from running the game.

cheers :P

avatar image KittenSnipes · Jan 16, 2018 at 12:16 PM 0
Share

@EliteHedgehog56

I hope it works.

Show more comments

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.

Follow this Question

Answers Answers and Comments

74 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

deactivate/activate child/separate prefab from script attached to other object 2 Answers

Enabling and Disabling Shurken Particles 1 Answer

displaying text problem 1 Answer

How to disable halo when it is already enabled via script C# 0 Answers

Enable and disabled parent contraint with c# 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