Search Unity

Finding a variable or Gameobject in a Array of Scripts

Discussion in 'Scripting' started by Jenifer_Jane, Sep 13, 2017.

  1. Jenifer_Jane

    Jenifer_Jane

    Joined:
    Jan 24, 2017
    Posts:
    69
    Hi, I am trying to find a Script with a part of its name since i have so many Scripts But only common thing in them is word "AI" after finding that script i want to access its variables and methods is their any way to do that.. I know another way is check if that gameobject got that script or not but i want to find it with its part of name i hope code bellow can explain better what i am trying to do.

    foreach (Collider c in HitCollider.Length)
    {
    // Made a Array of MonoBehaviours
    MonoBehaviour[] mono;

    // add scripts mine target have.
    mono = c.GetComponents <MonoBehaviour> ();

    // then checking each of them
    foreach (MonoBehaviour m in mono)
    {
    // finding script i want
    if (m.name.Contains ("Ai"))
    {
    // now i want to access this Script M's variables, Game objects and Methods i want to know how to do that.
    }
    }
    }

    any help would be appreciated thnx in advance.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    First and foremost, use code tags:
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    Code (csharp):
    1.  
    2. foreach (Collider c in HitCollider.Length)
    3. {
    4.     // Made a Array of MonoBehaviours
    5.     MonoBehaviour[] mono;
    6.  
    7.     // add scripts mine target have.
    8.     mono = c.GetComponents <MonoBehaviour> ();
    9.  
    10.     // then checking each of them
    11.     foreach (MonoBehaviour m in mono)
    12.     {
    13.         // finding script i want
    14.         if (m.name.Contains ("Ai"))
    15.         {
    16.             // now i want to access this Script M's variables, Game objects and Methods i want to know how to do that.
    17.         }
    18.     }
    19. }
    20.  
    Next, is there some consistent name of the variables from script to script? Like is it no matter if it's AiX or AiY, is the variable the same name? Or is it different depending the script?

    In the former there's a clear answer.

    In the ladder, well... that's weird. It logically falls apart very fast algorithmically.

    SO...

    In the former, the best way to do this is via polymorphism. And there are 2 basic ways to do this.

    1) have a base type from which your various AI script inherit from which defines the methods
    2) have an interface that each AI script implements

    Personally I prefer option 2, since it allows for a lot more wiggle room in the implementation, instead of restricting you to a class hierarchy.

    But I'll give examples of both...

    Option 1:
    Code (csharp):
    1.  
    2. public class AIBase : MonoBehaviour
    3. {
    4.  
    5.     public string SomeProperty;
    6.  
    7.     public virtual void Foo()
    8.     {
    9.      
    10.     }
    11.  
    12. }
    13.  
    14. public class AITypeA : AIBase
    15. {
    16.  
    17.     public override void Foo()
    18.     {
    19.         //do unique stuff
    20.     }
    21.  
    22. }
    23.  
    24. foreach(Collider c in HitCollider.Length)
    25. {
    26.     foreach(var ai in c.GetComponents<AIBase>())
    27.     {
    28.         ai.DoStuff();
    29.     }
    30. }
    31.  
    Option 2:
    Code (csharp):
    1.  
    2. public interface IAIScript
    3. {
    4.  
    5.     string SomeProperty { get; set; }
    6.  
    7.     void Foo();
    8.  
    9. }
    10.  
    11. public class AITypeA : MonoBehaviour, IAIScript
    12. {
    13.  
    14.     [SerializeField()]
    15.     private string _someProperty;
    16.  
    17.     public string SomeProperty
    18.     {
    19.         get { return _someProperty; }
    20.         set { _someProperty = value; }
    21.     }
    22.  
    23.     public void Foo()
    24.     {
    25.         //do unique stuff
    26.     }
    27.  
    28. }
    29.  
    30. foreach(Collider c in HitCollider.Length)
    31. {
    32.     foreach(var ai in c.GetComponents<IAIScript>())
    33.     {
    34.         ai.Foo();
    35.         ai.SomeProperty = "BLARGH"; //manipulating the property SomeProperty
    36.     }
    37. }
    38.  
    (note - the 'Collider in HitCollider.Length' is realy weird... I'm assuming you sort of mixed up for(int i = 0; i < HitColliders.Length; i++) and a foreach setup... but I just copied your syntax cause... yeah)
     
    Jenifer_Jane likes this.
  3. Jenifer_Jane

    Jenifer_Jane

    Joined:
    Jan 24, 2017
    Posts:
    69
    yea that was typo saw that after posting Corrected in mine project already forgot to change here btw thnx. My Problem Fixed.