Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Access to another script variable or method in the same gameObject

Discussion in 'Scripting' started by RealidadEmpoderada, Jan 26, 2017.

  1. RealidadEmpoderada

    RealidadEmpoderada

    Joined:
    Sep 28, 2016
    Posts:
    13
    If I have a scrpit with name: pipi
    a method in pipi called: public int pipiIn()
    and a variabel defined in pipi called : int pepo
    The gameObject name may be: PAPA

    How to accesss to the variable and the method from an other script of teh same gameObject?
     
  2. ELeigh

    ELeigh

    Joined:
    Dec 26, 2014
    Posts:
    2
    Although you can find the answer to that question pretty fast with google...

    You need to get the Script from the other with GetComponent:

    Code (CSharp):
    1. gameObject.GetComponent<pipi>().pepo

    and
    Code (CSharp):
    1. gameObject.GetComponent<pipi>().pipiIn()

     
  3. RealidadEmpoderada

    RealidadEmpoderada

    Joined:
    Sep 28, 2016
    Posts:
    13
    I know that GetComponent must be used. But there is any problem to reference teh gameObject.

    But now is solved. The problem is that I was calling from a script of of a State Machine Controller....
     
    Last edited: Jan 26, 2017