Search Unity

Executing script functions in other objects?

Discussion in 'Scripting' started by SpyridonZ, Apr 4, 2010.

  1. SpyridonZ

    SpyridonZ

    Joined:
    Jun 7, 2009
    Posts:
    97
    Let's say I'm using the following code..

    Code (csharp):
    1. g_Object = GameObject.Find("Swordman");
    My goal is to use an "Attack" function in the swordman, but if I try...

    Code (csharp):
    1. g_Object.Attack();
    That does not work, it gives me the error of Method Not Found, because I did not locate the script attatched to the Swordman object.

    My problem is, every unit in the game runs a different script, so I'm not sure how to locate it, considering the name is not static. Swordsman is not intended to attack like other units.

    Is there any way I could execute the function on the object without first locating the script? Or is there a way I could locate a script without knowing the name?

    My only solution atm is to name the script exactly after the game object, but I'm hoping to find another solution that is a bit easier, because that will make me need to reformat my project a bit.
     
  2. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    You grabbed a reference to the game object, but in order to execute a function in a script you also need a reference to the script component itself.

    This is done via GetComponent.
     
  3. SpyridonZ

    SpyridonZ

    Joined:
    Jun 7, 2009
    Posts:
    97
    Thanks for the response.

    Yep I got that part.

    What I was asking was more along the lines of... is there a way I could search for a specific type of component - meaning find any script component attatched to the gameobject - assuming I do not know the name of that script to search for it?

    Because the g_Object.Attack() command is used any type a unit attacks - so one time it may be telling a Swordsman to attack, another time it may be telling a Pikeman to attack - in which case it would not be able to search for the same script by name, because both units use different scripts.

    I hope that makes sense =)
     
  4. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    You could do some complicated voodoo with caching a reference in a separate script on the object or approach like that.

    Or you could leverage the OO features and simply make all of your different units inherit from a base class. So say you have a base class called Unit. You subclass pikemen, swordsman or archer, overriding functions as you see fit.

    Code (csharp):
    1. unitScript = GetComponent(Unit);
    You simply search for the base class. Then that should return the proper script, regardless of it it's a pikeman or an archer.
     
  5. atsd

    atsd

    Joined:
    Feb 23, 2010
    Posts:
    73
    If it none of them is good for you, also you have no speed issues.
    You can use g_Object.SendMessage("Attack",parameters,param,param....)

    you dont need to know script's name it basically send message to all children.
    But Let me warn you this is way slower than Quietus' method. But of course easier to implement.