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

how to call this object from another script ? c#

Discussion in 'Scripting' started by m-y, Feb 19, 2015.

  1. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    i want to call this (ball )
    from another script
    how can i do that ?
    call.png
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Local variables are only accessible from within the same method.
     
  3. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    you can pass it through a custom return function in another script
     
  4. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    how to create this custom return function ?
     
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Well, what do you need to call on that ball? What are you trying todo?
     
  6. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    position , rotation , scale
     
  7. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    You want to manipulate the position, rotation, and scale of that object? From another script?

    Script A:

    Code (CSharp):
    1. void Update()
    2. {
    3.        Vector3 ball = transform.position;
    4. }

    Script B:

    Code (CSharp):
    1. Public Vector3 ChangePos(float x, float y, float z)
    2. {
    3. Vector3 updatePos = new Vector3(x, y, z);
    4. return updatePos;
    5. }

    How to use it?

    Code (CSharp):
    1. Void Update()
    2. {
    3. Vector3 ball = ScriptB.ChangePos(value, value, value);
    4. }
     
  8. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    You can also just make the ball public and access it directly.
     
    ZO5KmUG6R likes this.
  9. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Encapsulation, DRY coding! Making variables public class variables just to make life a little easier can quickly turn into nightmares, imo!