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 DEloRius · Dec 24, 2014 at 01:10 PM · playerlife

3 Lives GUI

I have 2 separate scripts, one with the value of how many lives the player has left and spawn the player when they trigger it, the other is attached to the GUI hearts I have in my canvas to remove hearts when the player dies. My problem is this, I cant seem to get the GUI script to read how many lives I have left, when the integer goes down to 2 the hearts stay 3. I do not know whether this is a problem with my scripts, but here they are just in case their the cause:

PlayerRespawn

 public int LevelNumber;
 public GameObject player;
 public Transform playerSpawnPoint;
 public int LIVES = 3;

 
 void OnCollisionEnter2D(Collision2D target)
 {
     if (target.gameObject.tag == "Player")
         
         
         LIVES -= 1;
     {
         switch (LIVES)
             
         {
         case 3:
             target.transform.position = playerSpawnPoint.position;

             break;
             
         case 2:
             target.transform.position = playerSpawnPoint.position;

             break;
             
         case 1:
             target.transform.position = playerSpawnPoint.position;
         
             break;
             
         case 0:
             Application.LoadLevel(LevelNumber);
             break;
             
         }
     }
     
     
 }

}

HeartControl

 public int LIVES;
 public Texture2D Heartlife1;
 public Texture2D Heartlife2;
 public Texture2D Heartlife3;

         


 void Update () 
      {
     Boundary.PlayerRespawn.LIVES ();
         switch(LIVES)


         {
         case 3:
             guiTexture.texture = Heartlife3;
             break;
             
         case 2:
             guiTexture.texture = Heartlife2;
             break;
             
         case 1:
             guiTexture.texture = Heartlife1;
             break;
     
             
         }
     }
     
     
 }
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
0

Answer by SkaredCreations · Dec 24, 2014 at 01:12 PM

Why have you LIVES in both scripts? It should be only in one of them, and the other must read the value from the script that has it. For example leave it only in PlayerRespawn and then in HeartControl removes it and in your "switch" statement use Boundary.PlayerRespawn.LIVES (without the parenthesis, because they mean you're calling a function and not accessing to a property, so remove the line 11)

Comment
Add comment · Show 5 · 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 DEloRius · Dec 24, 2014 at 02:22 PM 0
Share

Changed the HeartControl to :

 void Update () 
      {

     switch (Boundary.PlayerRespawn.LIVES)


         {

and get this messege:

Assets/Scripts/HeartControl.cs(16,25): error CS0103: The name `Boundary' does not exist in the current context.

Am I referencing the Boundary right, it's a game object with just a collider on it.

avatar image SkaredCreations · Dec 26, 2014 at 09:20 AM 0
Share

But I don't see any Boundary declared or assigned in the HeartControl script, so where are you setting it?

avatar image DEloRius · Dec 26, 2014 at 04:02 PM 0
Share

I am new to codeing, when you say declare Boundary am I supposed to have a public Boundary; at the top?

avatar image SkaredCreations · Dec 26, 2014 at 07:30 PM 0
Share

If you have this code in PlayerRespawn:

 public static PlayerRespawn instance;
 
 void Awake () {
    instance = this;
 }

Then in HeartControl you can do:

 void Update () 
 {
     if (PlayerRespawn.instance != null)
     {
         switch(PlayerRespawn.instance.LIVES)
         {
              case 3:
                  guiTexture.texture = Heartlife3;
                  break;
                  
              case 2:
                  guiTexture.texture = Heartlife2;
                  break;
                  
              case 1:
                  guiTexture.texture = Heartlife1;
                  break;
          }
     }
 }
avatar image DEloRius · Dec 28, 2014 at 04:15 PM 0
Share

Thank you, this does now make my scripts talk to each other :D. I seem to have run into a brick wall with unity 4.6, I have used the up to date HUDcanvas and I think I need to change the guiTexture.texture to something else, the GameObject that the script is attached to has a Image (Script) which displays the 3 hearts, I cant find online how to link that in place of the guiTexture bit.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to increase the life of the player? 2 Answers

Life Bar controlled 1 Answer

Regain health on GUI 2 Answers

Life Player loads another level 1 Answer

How i can add a life for the user if he buy it? 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