Search Unity

Can I set variables for other scripts?

Discussion in 'Scripting' started by spritesobhan, Apr 30, 2017.

  1. spritesobhan

    spritesobhan

    Joined:
    Mar 28, 2017
    Posts:
    73
    ScriptA:
    public GameObject Player1;
    public Player player;

    void Start(){
    Player1 = GameObject.FindWithTag("Player1");
    player = Player1.GetComponent<Player>();

    ScriptB.player = player;
    }

    ScriptB:
    public Player player;
     
  2. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    I honestly have no idea what you're asking. Yes, any public instance variable can be set from another script given you have a reference to an instance of that other script. You should be able to set player in ScriptB from ScriptA.
     
  3. Varhius

    Varhius

    Joined:
    Oct 16, 2013
    Posts:
    4
    Yes you can, but you need a reference to Script B.

    For example:
    GameObject.FindWithTag("GameObjectWhereScriptBIsAttached").GetComponent<ScriptB>().player = player;
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775