c# - Unity3D: Accessing a Method from the only script available in a transform -


scenario:

i have 10 objects in scene. each object has unique class attached. on clicking 1 object, 2 methods need called.

  • a method in script attached clicked gameobject. (to enable newly clicked object's ui)
  • a method clicked gameobject. (to disable clicked object's ui)

what i've done:

if (physics.raycast(ray, out hit)) { hit.transform.getcomponent(hit.transform.name+"").show_status (true); current_transform.getcomponent(current_transform.name+"").show_status(false); current_transform = hit.transform; } 

here, trying access script using string script named after gameobject itself. method named "show_status()" available classes. above code produces below error.

"unityengine.component' not contain definition `show_status"

also, if isn't possible way i'm attempting it, helpful mention other simple approaches solving scenario.

accessing gameobject name slow. hit.transform.name slow , allocates memory each time called. getting component string name(getcomponent(string)) slow too.

if isn't possible way i'm attempting it, helpful mention other simple approaches solving scenario.

this can done interface.

create simple script called iclickable this not class. interface.

public interface iclickable {     void show_status(bool value); } 

your 10 scripts(implement iclickable in each one):

script1 called objecttoclick1.

public class objecttoclick1 : monobehaviour, iclickable {     //implement interface function     public void show_status(bool value)     {         //your implementation here     } } 

script2 called objecttoclick2.

public class objecttoclick2 : monobehaviour, iclickable {     //implement interface function     public void show_status(bool value)     {         //your implementation here     } } 

script3 called objecttoclick3.

public class objecttoclick3 : monobehaviour, iclickable {     //implement interface function     public void show_status(bool value)     {         //your implementation here     } } 

...

...

now, clicking part , detecting object clicked:

public class objecttest : monobehaviour {     transform current_transform;      // update called once per frame     void update()     {         if (input.getmousebuttondown(0))         {             //raycast mouse cursor pos             raycasthit raycasthit;             ray raycast = camera.main.screenpointtoray(input.mouseposition);             if (physics.raycast(raycast, out raycasthit))             {                 iclickable tempiclickable = raycasthit.transform.getcomponent<iclickable>();                 if (tempiclickable != null)                 {                     tempiclickable.show_status(true);                     current_transform.getcomponent<iclickable>().show_status(false);                     current_transform = raycasthit.transform;                 }             }         }     } } 

you can add many interface possible. flexible , can used detect types of enemies available.


Comments