Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Call method from script applied to multiple objects.

Discussion in 'Scripting' started by Darkmyst, Jan 30, 2015.

  1. Darkmyst

    Darkmyst

    Joined:
    Aug 3, 2013
    Posts:
    35
    So i have a script on one object that calls a method from another object to reset its position, but i have lots of these objects and im not too sure what to do.

    Ill just post an image below just to demonstrate what i mean:

     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I'm not sure what you are asking, but if you only want it to affect one object, you make a prefab and it will be kept separate.
     
  3. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    I know they say a picture is worth a thousand words.... but sometimes you still need a couple more words.

    Are you asking how you can assign all 4 of those red blocks so that they are all affected by the Collisions script?

    Rather than a single EnemyPatrol variable, place a List of them, then call the method on each of them.

    Code (csharp):
    1. using System.Collections.Generic; //needed for Lists
    2.  
    3. [SerializeField]
    4. private List<EnemyPatrol> enemyPatrols = new List<EnemyPatrol>();
    5.  
    6. //then when you call the method on the patrols....
    7.  
    8. private EnemyPatrolMethod(){
    9.     for (int i=0;i<enemyPatrols.Count;i++){
    10.          enemyPatrols[i].ResetPosition(); //or whatever method you use.
    11.     }
    12. }
    Then in the Inspector, make your List size, in this case, 4. Drag each EnemyPatrol into one of the four fields now available.

    If you were asking about something else, let me know.
     
    Random_Civilian likes this.
  4. Darkmyst

    Darkmyst

    Joined:
    Aug 3, 2013
    Posts:
    35
    Sorry for not being very clear. The problem im having is that i would usually just drag one of the enemies, which contain 'enemy patrol' to the variable box in the editor, but now i have more than one enemy so i don't want to have to use an array of something i just want to call them all at the same time.
     
  5. Darkmyst

    Darkmyst

    Joined:
    Aug 3, 2013
    Posts:
    35
    Sorry for not being very clear. The problem im having is that i would usually just drag one of the enemies, which contain 'enemy patrol' to the variable box in the editor, but now i have more than one enemy so i don't want to have to use an array of something i just want to call them all at the same time.
     
  6. Darkmyst

    Darkmyst

    Joined:
    Aug 3, 2013
    Posts:
    35
    If anyone still can't understand what i mean i can quickly make a video clearly demonstrating what the problem is.
     
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Through scripting you would give them a tag and then use GameObject[] enemies = GameObject.FindGameObjectsWithTag("whatever");
     
  8. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Thats what I thought, but I couldnt tell for sure. My solution does what you want - You can drag each enemy into a different slot into the list. Then in your code, you iterate over the list and perform your function on each member.

    I'm not a big fan of "FindGameObjectsWithTag" as it scales horribly with number of tags and an object can only have one tag.
     
  9. Darkmyst

    Darkmyst

    Joined:
    Aug 3, 2013
    Posts:
    35
    Im a little confused why you are serializing the private field, why not just make it a public list instead?
     
    Last edited: Jan 30, 2015
  10. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Unfortunately, Unity (or at least a lot of tutorials on Unity) seems to promote the overuse of public variables because of the Inspector.

    • [SerializeField] private means that the variable is private, but can be adjusted in the inspector.
    • public means that the variable can be adjusted in the inspector AND by anyone else who has access to this class.

    Unless you have a need to have direct access to the variables from other classes, its cleaner and safer to use private variables - and even if you DO want to access it from other classes, properties or Methods are often are a better option.

    http://broadcast.oreilly.com/2010/01/understanding-c-why-make-thing.html

    This article does a decent job of explaining why you would want to make things private.
     
  11. Darkmyst

    Darkmyst

    Joined:
    Aug 3, 2013
    Posts:
    35
    Thankyour for that link, helped me learn a little bit more about programming but when i use your script i get the error '
    Assets/Scripts/Collisions.cs(50,38): error CS1061: Type `System.Collections.Generic.List<EnemyPatrol>' does not contain a definition for `ResetPos' and no extension method `ResetPos' of type `System.Collections.Generic.List<EnemyPatrol>' could be found (are you missing a using directive or an assembly reference?)'

    From what i read up on on lists i think i need to implement something into the EnemyPatrol class ive tried messing around with this but i can't seem to figure out what i'm quite doing wrong though.
     
  12. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Well, if you are dragging objects onto the editor, I don't think scaling will be a problem.