Search Unity

Create an Array from script B that has access to the script A

Discussion in 'Scripting' started by MedalHellWay, May 1, 2019.

  1. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Hello everyone! :)

    It will be a very easy question, but I haven't found an answer anywhere... I've create two script, A and B.

    The A script as lot information regarding the gameobject in which it's attached; Name, position etc. It also contains a "fake" trigger which I need to understand if it's hit by the player (a bool Check variable). if the player collide the "Check" variable is true and viceversa. The script A is a basic script for all my gameobject in the scene.

    The B script is attached to the player and is useful for performing actions when the A script returns a true or false check... Well, here the problem arises

    When I access the script A from script B, I need to create an array of all the game objects that have attached the script A since the solution I adopted allows me to access only one object.
    If I building a Array in B the error that the debug gives me is that I cannot access the variable in an implicit way since there is no directive etc ..

    Es:

    Script A:

    Code (CSharp):
    1. public class ScriptA: MonoBehaviour
    2. {
    3.     public bool Check;
    4.     /// here more code...
    5.     if (Check == true)
    6.     {
    7.       Debug.Log ("true")  
    8.     }
    9.     else Debug.Log ("false")
    10.  
    11. }
    Script B (work well only with a gameobject)

    Code (CSharp):
    1. public class ScriptB: MonoBehaviour
    2. {
    3.     public ScriptA testA;
    4.  
    5.     void Start()
    6.     {
    7.       testA.Check = true
    8.     }
    9. }
    But if I try to build an array as shown below..

    Code (CSharp):
    1. public ScriptA[]  testA;
    I've error to access the variable in an implicit way since there is no directive etc ..

    In general, the player's script must contain all the objects in the scene in the inspector to access it...

    Solutions? Tips? I honestly can't figure out how to build an array by referring to an external script..

    Thanks in advance :)
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    What error do you get, and I don't believe you couldn't find anything, maybe search harder :D There ar etons of threads and tutorials on how to access another script and besides that a lot of threads about arrays, but nevertheless, what error do you get there. On what line. So right now you have an empty array in your last code part, its just an array that is empty, where do you add them? In the inspector in editor mode?
     
    MedalHellWay likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    In the ScriptB, if all you did was change the variable from a single target to an array, it's not going to just work. Now all your calls against a single target are trying to do things to the array and not to a single object in that array. So you'll have to change your other code to access an index in that array.
     
    MedalHellWay likes this.
  4. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    In general I have no problem interchanging data with multiple scripts. I have read many discussions, and above all here, I find many answers :) The list of gameobject must show in the Inspector. Now I don't have the original scripts at hand, since I now write on another computer. Tomorrow I will post the original scripts with error, if they can help...
     
  5. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Thanks for the help, but I think that by seeing the original scripts you better understand what I'm looking for exactly :) Thanks again :) Tomorrow update!
     
  6. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Ok, I've "solve" the problem for array. I managed to create an array with "FindObjectsOfType" in script B...

    Code (CSharp):
    1.  public ScriptA[] go;
    2.  
    3.     private void Start()
    4.     {
    5.         go = FindObjectsOfType(typeof(ScriptA)) as ScriptA[];
    6.     }
    This helps me to search and store all the gameobjects with the scriptA attached to it and the scriptB shows all objects in the inspector as array.

    Then with loop "For"...

    Code (CSharp):
    1. for (int i = 0; i < go.Length; i++)
    2.             {
    3.                 if (go[i].Check == true)
    4.              }
    I can get the bool variable Check.

    Another question; Why if I use "foreach" loop can't I access the Check directly? I've the error CS1061 as shown below:

    " 'scriptA[]' does not contain a definition for 'Check' and no accessible extension method 'Check' accepting a first argument of type 'scriptA[]' could be found (are you missing a using directive or an assembly reference?) "

    I know that if I use "FindObjectsOfType" I can directly access the properties of the script, object, components, etc.