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 Aspect13 · Dec 11, 2018 at 05:06 PM · multiple objectsaccessing scripts

Accessing ONE script attached to multiple GameObjects.

So I have 3 variables that I want to access and they are in one script. I could make it from another script but I am really curious how to make it and the code wold be clearer and more sensible where I want it to be. I tried to do it like that:

 EnemyController[] enemyController;
 enemyController = GameObject.FindGameObjectsOfType<>EnemyController>();

but then when I want to access these variables I can put only one number in these brackets: []. If you are curious what I'm doing and maybe it can make it clearer what I mean, then I want to decrease my player's HP each time the enemy hits him. I have 3 enemies and 3 variables: hellephantDmg, bearDmg, bunnyDmg. The scripts EnemyController is attached to all three of them. I want to make if statement and detect which enemy player hits that's why I want to access these 3 variables but the problem is I don't know how... Please help.

Comment
Add comment · Show 4
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 hexagonius · Dec 11, 2018 at 05:11 PM 0
Share

this sounds too complicated. who hit you should be clear in the method that is called inflicting the damage

avatar image Aspect13 hexagonius · Dec 11, 2018 at 05:15 PM 0
Share

I want to make variables ins$$anonymous$$d of hardcoding it but I guess you suggest to just decrease my hp by a specific value like:

 hp = hp - 25;



avatar image fafase · Dec 11, 2018 at 05:15 PM 1
Share

Are you not using OnCollisionEnter? Then you'd know which hits the player and no need to look for all EnemyController:

 void OnCollisionEnter(Collision col)
 {
      EnemyController enemy = col.gameObject.GetComponent<EnemyController>();
      if(enemy == null) { return; }
      int damage =  enemy.GetDamage();
 }

Then you can apply the damage to the player.

avatar image Aspect13 fafase · Dec 11, 2018 at 05:36 PM 0
Share

Thank you very much. Works perfectly. I'm quite new and I did not know how to get the EnemyController component from my enemies. I was using OnCollisionEnter() but as I said I didn't know how to access the script which is attached to multiple GameObjects. I would never think you can put "col" in this situation before

 gameObject.GetComponent<EnemyController>();

1 Reply

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

Answer by Vega4Life · Dec 11, 2018 at 05:18 PM

I think this is a good spot to learn some inheritance/polymorphism.


You have an EnemyController and it only needs to have a single variable of 'hitpoints' in it. Then, you create three other scripts called "Hellephant", "Bear", "Bunny". BUT, all three of these scripts derive from EnemyScript. Thus, they get access to their own 'hitpoints' (just make it protected), AND they are considered an EnemyController.


This way, you can store all your enemies as an EnemyController and get a single point of access to 'hitpiont'. If you need to know if if an EnemyController is a specific enemy, you do if (EnemyController is Hellaphant) { "do things specifically to hellaphant"};


I went through it pretty fast, but it should get the idea across.


Comment
Add comment · Show 2 · 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 Aspect13 · Dec 11, 2018 at 05:39 PM 0
Share

So you mean I have to create 3 new scripts without any code in them? I don't know what's polymorphism neither inheritance. I also guess I have to change something in this part of script

 public class Hellephant : $$anonymous$$onoBehaviour

and make it like that:

 public class Hellephant : EnemyController

am I right?

avatar image Vega4Life Aspect13 · Dec 11, 2018 at 05:56 PM 0
Share

You are correct. Inheritance just means (in simplest terms), it gets the class members and methods of its derived class - in this case EnemyController. It's like a copy paste. In other terms, it's like a dog is a mammal. $$anonymous$$ammals has all kinds of attributes - eyes, ears, legs. Because a dog is a mammal, the dog will have those same attributes a mammal has.


Polymorphism means that Hellephant is an enemy (because it derives from it). So anytime code is looking for an Enemy, you can pass Hellephant as the data type. The reverse on the other hand, isn't true. An Enemy isn't always a hellephant. So if code is looking for a hellephant, you can't just pass an enemy to it.


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

97 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

Related Questions

GUITexture multiple component activation 1 Answer

OnTriggerEnter Script only working on duplicated object 1 Answer

How to load multiple entries from one Binary file? 1 Answer

How to access other scripts from one script 0 Answers

Custom Editor Access to Scene Objects Variables (in the same way as UnityEvents) 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