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 dhruv777 · Aug 16, 2017 at 11:07 AM · scripting problemgameobjectdebugcodepagedeclaration

Need to increase the gameobject value .

I want to increase the value of extralives when player comes in contact with the health sprite. I have setup others details like box colllider and is triggered perfectly. I just want to know the command to increase a game object value (i.e. UIExtraLives increment by 1) . Each life is represented by an Health sprite and not the text . Bottom right down is the shown window for lives in the image. alt text

 public GameObject[] UIExtraLives;
 
 public void AddLife(int amount)
     {
 
         // update UI
         int i = UIExtraLives.Length;
         i++;
         UIExtraLives.Length [i].SetActive (true);
 
     }


screenshot-20.png (338.2 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by M-G-Production · Aug 16, 2017 at 05:44 PM

First: Your player GameObject and your health sprite GameObject must both have a collider 2D. The Health Collider2D must be set as Trigger.

Then in your health object, Add a script with these lines of codes:

 public void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.GetComponent<PlayerScript>())
         {
             other.gameObject.GetComponent<PlayerScript>().AddLife(1);
             //If you want to destroy the object...
             Destroy(gameObject);
         }
     }

And of course, change the PlayerScript to the proper name of the script containing the public void AddLife(int amount).

Then you should change your codes in AddLife(int amount):

 public GameObject[] UIExtraLives;
 public int actualExtraLives = 1, maximalExtraLives = 5; 
 
  public void AddLife(int amount)
      {
         if (actualExtraLives < maximalExtraLives)
             actualExtraLives += amount;
         if (actualExtraLives > 0)
             UIExtraLives[(actualExtraLives - 1)].SetActive (true);
      }


Comment
Add comment · 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
0

Answer by dhruv777 · Aug 17, 2017 at 10:13 AM

thanks for the reply. I want to know one more thing .

why u wrote

       if (actualExtraLives < maximalExtraLives)
          actualExtraLives += amount;
      if (actualExtraLives > 0)
          UIExtraLives[(actualExtraLives - 1)].SetActive (true);

Isn't there in command where we can increase the value of UIExtraLives in just one line.

Like :

             UIExtraLives[ (initial valual of UIExtraLives + 1)].SetActive(true);

Sorry for asking this but I just started coding a while back.

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 M-G-Production · Aug 17, 2017 at 03:55 PM 0
Share

First, I guessed that there must be a limit to your extra lives, so I created a top limit (maximalExtraLives)

And yes, of course you can add a value to your array! But it doesn't seems like what you want to do, because it looks like you dragged and dropped some GameObjects in your array... Is the Lenght of the array already defined? If so the maximal limit represent the 'Lenght' so it won't throw an error!

But the code you are looking for is:

 UIExtraLives[(UIExtraLives.Lenght+1)];


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

135 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

Related Questions

How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer

Why are these object passing through each other? 1 Answer

Calculating Scrolling GameObject x position scrolling pass another GameObject x postion (2D Game) 1 Answer

How to fix a Missing Refrence Exception Error 1 Answer

Update list on mouse click 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