Calling a Function in a Specific Instance

Here’s my objective: I’m trying to get a controller to tell each instance to Lerp itself.

Let’s say I’ve got 3 instances of an object named “Cube” out in the game world. We’ll call these instances 001, 002, and 003. Now, I have a controller object that needs to call a function named “moveUnit” in “Cube”. The trick is, I only want to call moveUnit on an individual instance, not all “Cubes”.

if (Input.GetMouseButtonDown(1)) {
if (selectedUnit != null) {
selectedUnit.GetComponent("HelloWorld").moveUnit(movePos);
 }
}

In the above code, “selectedUnit” is a reference to one of the cube instance. SelectedUnit could hold instance 001, 002, or 003. How can I call “moveUnit” on “selectedUnit”? As it is now, one call interrupts another.

The question in my mind is what sort of mechanic do you want to use to populate selectedUnit with a reference to one of the instances? You can start simple by declaring it as a public variable then drag-assigning one instance to that variable. But then you’ll need some way during the game to change that of course. Starting simple:

var selectedUnit : GameObject;

function Update () {
  if (Input.GetMouseButtonDown(1)) {
    if (selectedUnit != null) {
      selectedUnit.GetComponent("HelloWorld").moveUnit(movePos);
    }
  } 
}

Now drag assign one of the instances and away you go. Does that help get you started? If not then let us know what piece is missing so we can offer better help!

Much appreciated HiggyB. Yes, I’m aware of that basic method of assigning via the editor, but I’m looking for something totally dynamic.

Right now I’m doing a Raycast to see which unit the player clicked on in an RTS-type game. My problem is that I can’t anticipate which type of unit a player might select. It could be LargeTank or SmallTank or Boat. Regardless of what the unit is, I need to be able to grab a hold of its script and call the moveUnit() function. But I can’t do that because I can’t anticipate the script name (which is different for each object).

How would I go about solving this problem? I supposed it’s possible to attach a separate “moveUnit” script to every movable object… but that seems kludgy.

Also, I’m running into a problem even testing if the object clicked on is a movable unit or not. I thought about assigning a variable to every object called: “isSelectable” but then how would I access this variable from outside the object? My controller can’t doing something like:

selectedUnit.GetComponent("ScriptName").isSelectable

Because the script name is always unknown. It’s an off-shoot of the problem described above, and I can’t move forward until I figure it out. Any advise is appreciated greatly.

I wouldn’t call that a kludge myself, but so it goes. You can do this a few ways:

  • Use tags for each object, when you raycast and hit something you check its tag and then based on that call the proper script.

  • Expose a property in some commonly shared script, that property in instances of that type points to the component that does have a moveUnit method.

  • Or, have all these objects share a moveUnit method so this all goes away.

Good tips, I’ll want to try them all out tomorrow to see which one seems most elegant.

Does Unity have inheritance, such that I could create an abstract unit class called “unit” with a property like “isSelectedable” and then create children of that class like “smallTank”, “largeTank”, “gunShip”, etc. that will automatically have the property “isSelectable” along with whatever scripts “unit” has attached to it? That might be elegant.

Obviously parent’s x,y,z position should not affect a particular unit’s position, which is how I understand Unity’s parent/child relationship works.