Call method from script applied to multiple objects.

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:

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.

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.

using System.Collections.Generic; //needed for Lists

[SerializeField]
private List<EnemyPatrol> enemyPatrols = new List<EnemyPatrol>();

//then when you call the method on the patrols....

private EnemyPatrolMethod(){
    for (int i=0;i<enemyPatrols.Count;i++){
         enemyPatrols[i].ResetPosition(); //or whatever method you use.
    }
}

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.

1 Like

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.

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.

If anyone still can’t understand what i mean i can quickly make a video clearly demonstrating what the problem is.

Through scripting you would give them a tag and then use GameObject[ ] enemies = GameObject.FindGameObjectsWithTag(“whatever”);

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.

Im a little confused why you are serializing the private field, why not just make it a public list instead?

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.

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

Well, if you are dragging objects onto the editor, I don’t think scaling will be a problem.