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
1
Question by Metorphium · Nov 16, 2016 at 04:45 PM · c#functionsinstantiationreferencing

How can i reference the scripts of multiple Instantiated GameObjects?

Im kind of struggling calling a Function from my Enemy's Script when attacking. My current problem is not beeing able to reference the Script i need to call the Function on for the Enemy to get damaged. These Enemies are spawning over time while playing. How can this be achieved?

Comment
Add comment · Show 5
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 Desoxi · Nov 16, 2016 at 05:04 PM 0
Share

Well, there are several solutions to this. You could use tags and find those object via its tag through FindWithtag or if you dont want to use tags you could just use GameObject.Find("NameOfObject") and get the script you want a reference to via GetComponent().

Short example: YourScriptname temp = GameObject.Find("ObjectToFind").GetComponent(); temp.Some$$anonymous$$ethodYouWantToCall();

Another approach would be to let those instantiated objects register themselves into a central controller which holds a list of all current enemy objects. Its up to you which approach you want to use in considering how much control you really want to have. How dynamic your whole setup should be etc.

avatar image Metorphium Desoxi · Nov 16, 2016 at 05:24 PM 0
Share

Wouldn't i need to call GameObject.Find every all the time ? Because its normally called in Start but when the Enemy just spawned at some other time it wont get found.

Another Problem is that i would have only 1 Script at a Time wont i ? But i want to attack all enemies and not only one. I dont know how Lists work, i only know they work like Arrays even though im never really using Arrays either..

avatar image MaximumTre Metorphium · Nov 16, 2016 at 05:40 PM 0
Share

From what I understand, GameObject.Find is really heavy and you shouldn't use it a lot because it will slow your game down. You should really look up how to use Lists as they are perfect for this sort of thing.

If you are using this for enemies you could pool the enemies in a List then just 'target' all the active enemies in the list. Seriously though, learn how to use Lists, they are awesome!

Show more comments

3 Replies

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

Answer by Desoxi · Nov 16, 2016 at 06:29 PM

Jep @Metorphium, you are right, GameObject.Find would work but in a scene with a lot of objects it could heavily affect your performance. Then i would recommend to use the generic Lists approach. And its the even better solution if you want to damage ALL enemies. The only thing you would have to do is to tell the enemy scripts to register themselves in some kind of EnemyManager script. And when you want to damage all of them just create a method inside your manager to do exactly that with iterating through the list of enemies and call a method like Damage(50.0f) or something. Ill try to write a really short example for it:

EnemyManager.cs:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class EnemyManager : MonoBehaviour
 {
     public static EnemyManager instance = null;
 
     List<Enemy> enemies;
 
     void Awake()
     {
         if(instance == null)
         {
             instance = this;
         }
         else
         {
             Destroy(gameObject);
         }
 
         enemies = new List<Enemy>();
     }
 
     public void DamageAll(int damage)
     {
         foreach(Enemy e in enemies)
         {
             e.TakeDamage(damage);
         }
     }
 
     public void RegisterEnemy(Enemy enemy)
     {
         enemies.Add(enemy);
     }
 }
 

Enemy.cs:

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour
 {
     int hp = 100;
     
     void Start()
     {
         EnemyManager.instance.RegisterEnemy(this);
     }
 
     public void TakeDamage(int damage)
     {
         hp -= damage;
     }
 
 }
 
Comment
Add comment · Show 5 · 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 MaximumTre · Nov 16, 2016 at 11:44 PM 0
Share

This is what I replied with, only written much better lol. This seems like it should be a pretty standard way to do what $$anonymous$$etorphium wants.

avatar image Desoxi MaximumTre · Nov 17, 2016 at 08:35 AM 1
Share

Thanks for the compliment, but from what i saw in your code it should work as well. I tried to write something more generic so it can be applied to any other manager as well :)

P.S.: I accidentally voted down your answer because i was reading stuff on my smartphone xD Its up again :)

avatar image Desoxi Desoxi · Nov 17, 2016 at 08:36 AM 0
Share

Ofc you can write it with real generic types too.. :D

Show more comments
avatar image Desoxi · Nov 17, 2016 at 04:18 PM 0
Share

@$$anonymous$$etorphium If you want to hit ALL enemies one by one you can achieve that when you create a coroutine which would be called from DamageAll() which again would have a short breakt of, say 0.5f sec, each time it iterates through the for loop.

But if you just want to call a method on a object which you hit with a raycast i would recommend you to read the following docs: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html and use the "out hit" param. To get the gameObject you just hit you then have to use hit.gameObject.GetComponent(). You dont need a list for this approach.

avatar image
1

Answer by MaximumTre · Nov 16, 2016 at 07:42 PM

I'm not a pro at this, but here's an example for how to use a list:

     // Forgot Preprocessor stuff >:(
     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;

     List<GameObject> pooledEnemyList;
     public int pooledAmount;
     
     void Start()
     {
     pooledEnemyList= new List<GameObject>();
             for (int x = 0; x < pooledAmount; ++x)
             {
                 GameObject obj = (GameObject)Instantiate(pooledObject);
                 obj.SetActive(false);
                 pooledEnemyList.Add(obj);
             }
     
     void FindEnemies()
     {
     foreach(GameObject obj in pooledEnemyList)
     {
     // Do something here like...
 if(obj.active)
 {
 obj.GetComponent<SomethingYouWant>().SomeFunction();
 }
     }
     }
 

Sorry for crappy formatting.

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 Metorphium · Nov 17, 2016 at 04:32 PM

huh.... so i dont need a list afterall if i just normally want to damage 1 Enemy.... Well thats my bad, but Knowledge never is wrong. I may just keep the List for future capabilities.

I will read through that! Thank you!

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 Desoxi · Nov 17, 2016 at 06:09 PM 0
Share

You are welcome, glad i could help!

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

241 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Instantiated Objects dont get their Script referenced properly 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# utilities functions in other script 1 Answer

multi reference a specific game object to a script that it's attached to many game objects ? 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