• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Count multiple objects! 3 Answers

overlapping of enemy 2 Answers

How to make multiple, evenly-spaced objects follow any path with any number of points? 2 Answers

i have variable in camera controller script which is target :transform i want it to change as i click on objects.and be that the same gameobject's transform i clicked as target.what do i do? 1 Answer

Something is wrong with my code please help! 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges