Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Amroth_Calmcacil · Oct 22, 2014 at 11:43 PM · c#instantiateprefabloop

Instantiate prefab once help C#

Hi, I'm trying to spawn a health object when the players health is 15 or less but my instantiate creates a loop if I don't pick up the health object instantly. Could anyone help me please?

Code lines: 14, 15, 30, 31, 32, 33, 34, 35, 36, 37

 using UnityEngine;
 using System.Collections;
 
 public class Player_Health : MonoBehaviour {
 
     public static float curHealth = 100f;
     public static float maxHealth = 100f;
     public static float decHealth = 5f;
     public static float regHealth = 1f;
 
     public static bool levelFail = false;
     public GUISkin skin;
 
     public GameObject spawnHealth;
     public bool completedHealth;
 
     void Start(){
         curHealth = 1;
         }
 
     void Update(){
         if (curHealth < maxHealth) 
         {
             curHealth += regHealth * Time.deltaTime;
         }
         if (curHealth <= 0) 
         {
             levelFail = true;
         }
         if (curHealth <= 15) 
         {
             completedHealth = true;
             if(completedHealth = true)
             {
                 Instantiate(spawnHealth);
                 completedHealth = false;
             }
 
         }
     }
 
     void OnTriggerStay2D(Collider2D other){
         if (other.gameObject.tag == "Darkness") 
         {
             curHealth -= decHealth * Time.deltaTime;
         }
     }
     void OnTriggerEnter2D(Collider2D other){
         if (other.gameObject.tag == "Health") 
         {
             Player_Health.curHealth = maxHealth;
             Destroy(other.gameObject);
         }
     }
 
 
     void OnGUI(){
         GUI.skin = skin;
         string HealthText = "Your Health: " + curHealth + " / " + maxHealth;
         GUI.Box (new Rect(Screen.width /2 - 350, 20, 200, 24), HealthText);
 
         if (levelFail == true)
         {
             Time.timeScale = 0;
             Rect winScreenRect = new Rect(Screen.width/2 - (Screen.width *.5f/2), Screen.height/2 - (Screen.height *.5f/2), Screen.width *.5f, Screen.height *.5f);
             GUI.Box(winScreenRect, "Level Failed!");
             
             if (GUI.Button(new Rect(winScreenRect.x + winScreenRect.width - 170, winScreenRect.y + winScreenRect.height - 60, 150, 40), "Retry"))
             {
                 Destroy(gameObject);
                 Application.LoadLevel(Application.loadedLevel);
                 Game_Manager.lightsActive = 0;
                 curHealth = 100f;
                 levelFail = false;
                 Time.timeScale = 1;
                 EndLvl.curHealthGuard = EndLvl.maxHealthGuard;
             }
             if (GUI.Button(new Rect(winScreenRect.x + 20, winScreenRect.y + winScreenRect.height - 60, 100, 40), "Quit"))
             {
                 Application.LoadLevel(0);
             }
             
         }
     }
 
 }
 
Comment
Add comment · Show 2
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 HuskyPanda213 · Oct 23, 2014 at 12:39 AM 0
Share

The problem--I am assu$$anonymous$$g--is that the health object is being constantly spawn. If that is the case (which the code seems to show), the fix would be to remove: completedHealth = true; in the curHealth 15.

avatar image Habitablaba · Oct 23, 2014 at 12:53 AM 1
Share

Yeah, this here:

  if(completedHealth = true)
 

is not checking for equivalency, it is assigning true to completedHealth, then it is evaluating the result of that as a boolean expression (which will be true).

In the future, make sure you are using == if you want to check for equivalency. Also, you totally don't have to check for the equivalency of a bool value. Just use

 if(completedHealth)  

or

 if(!completedHealth)

1 Reply

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

Answer by MarksTeq · Oct 23, 2014 at 01:02 AM

Also change

  if (curHealth <= 15) 

to

 if (curHealth <= 15 && completedHealth == true)
 {
  Instantiate(spawnHealth);
 completedHealth = false;
 } 

and do this:

  void Start(){
 curHealth = 1;
 completedHealth = true;
 }

else it's just going to repeat infinitely because you always just set it back to true

Comment
Add comment · 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 Amroth_Calmcacil · Oct 23, 2014 at 05:36 AM 0
Share

Thank you very much for your help, it's all working now.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How do I Instantiate a prefab in a list? 0 Answers

OverlapSphere for parallel arrays 1 Answer

Instantiate prefab (button, texture) with attached script 1 Answer

Having multiple objects fire prefabs in different times C# 0 Answers


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