- Home /
How to play iTween event on all GameObjects tagged "example"
I have 25 objects in a scene, which need to all play an iTween event when a trigger elsewhere in the scene is triggered. I have the 25 objects tagged "MonolithicShard", and each has an iTween event named "FadeOut".
Rather than write a script that individually references every single object, how can I simply tell the trigger to find all objects tagged "MonolithicShard" and play the event "FadeOut"?
I know the script to call the event on the object the script is attached to is:
iTweenEvent.GetEvent(GameObject, "EventName").Play();
But although I've been reading all afternoon, I can't figure out how to trigger them all according to tag.
Thanks in advance!
Answer by dannyskim · Oct 17, 2012 at 03:14 PM
Well, there's a few ways you could go about doing this. The method that you are referring to is at the following link:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
GameObject[] shardArray = GameObject.FindGameObjectsWithTag("MonolithicShard");
for( int i = 0; i < shardArray.Length; i++ )
shardArray[i].SendMessage("yourTweenMethodName",SendMessageOptions.DontRequireReciever);
Or, if your MonolithicShard's have their own Script attached, lets say the class name is MonolithicShard, you could do:
MonoLithicShard[] shardArray = Object.FindObjectsOfType(typeof(MonoLithicShard)) as MonoLithicShard;
for( int i = 0; i < shardArray.Length; i++ )
shardArray[i].yourTweenMethodName;
But, but these methods are relatively slow. FindObjectsOfType is quite a few times slower than the tag method, but in turn you're calling methods directly on the references of the scripts rather than using SendMessage, which is slow too. Your best bet would be to use delegates and events, and subscribe to the event of the trigger.
So, on your object that needs to announce the trigger:
public delegate void ShardTweenHandler(GameObject hitGO);
public static ShardTweenHandler onTrigger;
void OnTriggerEnter( Collider other )
{
if( onTrigger != null )
onTrigger( other.gameObject );
}
To subscribe to the event from your mono lithic shard scripts, you would do:
void OnEnable() { ClassNameWithEventInside.onTrigger += onTrigger; }
void OnDisable() { ClassNameWithEventInside.onTrigger -= onTrigger; }
void onTrigger( GameObject hitGO )
{
// you can have your iTween in here
}
For a quick tutorial on delegates and events:
Thanks for your indepth answer, unfortunately I've not had a chance to implement any changes, but as soon as I do I'll come back and accept the answer. Didn't want you thinking I'd ignored it!
Your answer
Follow this Question
Related Questions
how to select which object i want to use in itween C# 0 Answers
how do I make a sound play when player controller hits a tagged item 1 Answer
how to use itween with the object attached to other moving object 0 Answers
Play sound on collision 1 Answer
How do I count or get references to all gameobjects that are tagged with a particular tag? 2 Answers