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 InSpirationhh · Aug 16, 2020 at 03:13 AM · 2d gametopdownreferencing

Apply the same script on multiple objects

Hello! So I've been working on the basic mechanics of a top-down shooter game, and now I'm trying to apply some of the scripts and components that work for a single object to multiple objects. However, some of them do not work as expected. For example, I have a health bar UI (canvas in world space) that displays the enemy health. It works perfectly fine for a single enemy, but when I duplicate the enemy object, along with the canvas, the canvas does not function properly. I changed the references so that shouldn't be the issue. I'm wondering what's the problem here. Thank you! (As you can see in the pic, the second health bar does not behave according to the script) alt text alt text

healthbarissue.jpg (233.3 kB)
issue2.jpg (159.3 kB)
Comment
Add comment · Show 8
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 Shrimpey · Aug 16, 2020 at 08:31 AM 0
Share

If you changed references then the problem must be in your script. Each gameobject has an instance of a $$anonymous$$onobehaviour script, so they should be working independently (unless you're modifying some shared variables, like $$anonymous$$aterial).

avatar image InSpirationhh Shrimpey · Aug 16, 2020 at 09:21 PM 0
Share

Hi! Thank you for your response! What you said makes sense but I still don't know how to solve the problem. The image attached is my script for my health bar. The functions inside are called in my EnemyController script, which applies to multiple enemies. Could you take a look and possibly find the error? alt text

healthbarscript.jpg (44.0 kB)
avatar image Shrimpey InSpirationhh · Aug 17, 2020 at 06:43 AM 0
Share

And how do you call SetEnemy$$anonymous$$axHealth and SetEnemyHealth? Also what is the exact problem, because second screenshot shows two healthbars above enemies, is it that one is full health all the time and not working properly while the other one is fine?

Show more comments
avatar image unity_MXGApNcapNfNLg · Aug 17, 2020 at 06:19 AM 0
Share

Can you paste the health script here?

avatar image InSpirationhh unity_MXGApNcapNfNLg · Aug 17, 2020 at 02:58 PM 0
Share

Hi! Can you please be a bit more specific? The health bar script is already shown in the description, so do the respective calls to set enemy health.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xxmariofer · Aug 18, 2020 at 07:29 AM

hello, the none working one is a duplicate? instantiated at runtime or by code? my bet is that both EnemyHealthBar are referencing to the exact same objects can you add this method to that EnemyHealthBar script and test?

 void Start()
 {
    slider = GetComponent<Slider>();
    fill = transform.GetComponentInChildren<Image>();
 }
Comment
Add comment · Show 6 · 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 InSpirationhh · Aug 18, 2020 at 06:18 PM 0
Share

Hi! Thank you for your response! I think what you said is exactly the problem. When the original enemy died (hence its health bar is also destroyed) and I shoot the duplicate enemies, error "$$anonymous$$issingReferenceException: The object of type 'Image' has been destroyed but you are still trying to access it" occurred. However, I still have no idea how to solve this other than changing references (which doesn't work). The healthbar is created in game, the code simply changes it.

avatar image xxmariofer InSpirationhh · Aug 18, 2020 at 06:39 PM 0
Share

Have you tested my Code? Should fix the errors

avatar image Shrimpey InSpirationhh · Aug 19, 2020 at 07:13 AM 0
Share

And how are you changing references? Double check if it's really pointing to 2 different health bars (especially the Fill image property). Cause it really does seem like both objects are pointing to the same object.

avatar image tubelightboy · Jul 04, 2021 at 06:43 PM 0
Share

Shouldn't we avoid getcompent as much as possible to lower the burden of cpu/ improve the performance? PC aside, if I am porting my game to android and If I have lot of getcomponent, chances are it is going to affect the performance.

avatar image xxmariofer tubelightboy · Jul 05, 2021 at 06:22 AM 1
Share

Adding "getcomponents" in the start of the game would never affect performance, only loading times. It is recommended to avoid getcomponents in update/fixedupdate/loops because thats called every frame.

Never be afraid of doing it in the starts/awake if that simplifies the code and working with the project, just to give you some numbers:

in this article you will see that calling 100.000 thousand times GetComponent taked him 20ms, even if you are targeting really low end devices, it is not common calling GetComponent more than 100 times unless is a big game and running the sample in a 4x times slower device it would take 0,0000008 seconds to do 100 getcomponent in a really low end device.

20ms overhead in the update is really high, but even for a loading window probably is imposible to notice.

avatar image tubelightboy xxmariofer · Jul 06, 2021 at 01:44 AM 0
Share

Oh, good to know. that's great then! thank you.

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

177 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

Related Questions

How to share a variable through multiple copys of the same script 2 Answers

2D Rotation for aiming problem 1 Answer

2d top-down rpg direction check 1 Answer

How to get an enemy to go to a location and then return. 1 Answer

Prefab to a tile script 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