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 Sansvern · Jul 06, 2021 at 01:00 PM · scripting problemhealthbar

Access to int from another script

I'm trying to make a script that checks your health and removes a heart if it goes down, but I can't seem to access to the character script's health. It's the following:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Corazones : MonoBehaviour
 {
     public Personaje personaje;
     public int SaludActual;
     public int TopeVida;
     // Start is called before the first frame update
     void Start()
     {
         SaludActual = personaje.vidaActual;
     }
 
     // Update is called once per frame
     void Update()
     {
         SaludActual = personaje.vidaActual;
         if(SaludActual < TopeVida)
         {
             (this.gameObject).SetActive(false);
         }
             
     }
 }

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

Answer by Devster2020 · Jul 06, 2021 at 02:21 PM

Hello @Sansvern
You need to change a few things..

First of all, the "Corazones" script will be move to a GameObject that can link all corazones.. just create a simple GO and call it "Corazones Manager", adding the corazones script.. it will be like this:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Corazones : MonoBehaviour
  {
      public Personaje personaje;
      public GameObject[] corazonesList;
      public int SaludActual;
      public int TopeVida;
  
      void Update()
      {
          this.SaludActual = personaje.vidaActual;
 
          for(int i = 0; i < this.corazonesList.Count; i++)
                 this.corazonesList[i].SetActive(this.SaludActual >= (i+1));
 
      }
  }


Additional Hint: This is not the best way.. the best one is to avoid the continuously check in the "Update" method and just call a function like "UpdateCorazonesGUI" when the player lose or gain a life..


I also take this opportunity to give you some tips on the style of the code.. here are some changes that make it more readable and clean (Player script):

in the Update method

 if(Inmune)
          {
              if(Time.timeSinceLevelLoad >= nextTimeKickInmune)
              {
                  AcabarInmunidad();
              }
          }


can be changed to:

 if(Inmune && Time.timeSinceLevelLoad >= nextTimeKickInmune)
             AcabarInmunidad();





And this is the "Muerto" function optimized:

      public bool Muerto()
      {
          return vidaActual <= 0;
      }

Comment
Add comment · Show 14 · 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 Sansvern · Jul 07, 2021 at 06:52 AM 1
Share

it doesn't work, it tells me Type UnityEngine.GameObject[]' does not contain a definition for Count'

avatar image rage_co Sansvern · Jul 07, 2021 at 06:56 AM 0
Share

it's a small mistake...... .Count is used for lists...arrays like what you're using use .Length

avatar image Devster2020 rage_co · Jul 07, 2021 at 07:01 AM 1
Share

Exactly.. thank for reply for me :)

avatar image rage_co Sansvern · Jul 07, 2021 at 06:57 AM 0
Share

you can either use a list instead of array..or just replace Count with Length

avatar image Sansvern rage_co · Jul 07, 2021 at 07:01 AM 1
Share

Okay, it seems to work, finally a number goes from 3 to 0 on the script, but the hearts don't disappear yet

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

220 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 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 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

Healing Zone. 3D game 2 Answers

How do I create an undertale player health bar in C #? 1 Answer

Smoothly move 2D Sprite 2 Answers

Why my Photon Scripts must be in a subfolder? 0 Answers

Cannot convert type 'UnityEngine.GameObject' to 'GameObject' 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