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

Gameobject that is used to prefab - to access other objects.

Discussion in 'Scripting' started by Dazz500, Dec 12, 2014.

  1. Dazz500

    Dazz500

    Joined:
    Nov 13, 2014
    Posts:
    64
    Hello all,
    I've been pulling my hair out trying to work out how to access other game objects from a script. Everything I tried didn't work, I've tried many example scripts that seemed easy enough but didn't work for me.

    I then found this http://answers.unity3d.com/questions/10857/how-can-i-access-other-scripts-and-their-functions.html
    I used the bat & ball example, I made a object called ball with a Ball script attached to it as in the example then tried to access the Ball script from my own game object like this
    Code (csharp):
    1.  
    2. public Ball ball
    3.  
    Ball was red with " error cs0103: The name 'Ball' does not exist in the current context.
    Even more frustrated I then decided to make a player object with a script called playerscript attached
    and the above code in it. This worked - I couldn't believe it.

    So the only difference between my object with my script and the ball script is that my object is used to instantiate other gameobjects.

    So this brings me to the question - how do I get a gameobject that is instantiated many times to connect to another game object. I would also need the instantiated gameobjects to connect ( pass variables to and get variables back ) to another gameobject when they are clicked.

    Thanks.
     
  2. Megolas

    Megolas

    Joined:
    Apr 6, 2013
    Posts:
    25
    Not sure if I understand the question, but you can use:
    GameObject theGameObjectWithTheScript = something;
    something.GetComponent<ScriptName>().aPublicFunctionInTheScript();
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    This means the compiler can't find the class named "Ball". Make sure the spelling and capitalization is exactly the same as in your Ball.cs script. If it is, then check the namespace. If Ball is defined in a namespace, you can add "using <namespace>;" to the top of your other script file so it can find Ball.

    @Megolas covered the other issue (accessing functions in a script on another object). You mentioned that the GameObject is instantiated many times. If you can't uniquely identify the right GameObject using GameObject.Find() or GameObject.FindObjectWithTag(), you may need to set a reference to it manually. For example, say there are many balls in a pitching machine, and there's one bat named "Bat". When the machine pitches a ball, it can find the bat and tell it which ball is in play:
    Code (csharp):
    1. // In the pitching machine's script:
    2. public void PitchBall(Ball ball) {
    3.     GameObject.Find("Bat").GetComponent<Bat>().activeBall = ball;
    4. }
    5.  
    6. ...
    7. // In Bat.cs:
    8. public Ball activeBall;
    9.  
    10. public void Update() {
    11.     if (activeBall != null) {
    12.         ZeroInOnPosition(activeBall.transform.position);
    13.     }
    14. }
    Often, though, you don't need to hold onto a reference. If you rely on OnCollisionEnter(), for example, you'll get a reference to the right GameObject as an argument.
     
  4. Dazz500

    Dazz500

    Joined:
    Nov 13, 2014
    Posts:
    64
    Hi thanks for your reply's.

    Spelling / capitalization is correct and it's not in a namespace, as I said I've got another script that can access it without a problem but this script can't.

    Any ideas ??
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Can you post both scripts? Ball.cs and the one that declares "public Ball ball;".
     
  6. Dazz500

    Dazz500

    Joined:
    Nov 13, 2014
    Posts:
    64
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Ball : MonoBehaviour {
    6.  
    7. public int testspeed = 100;
    8.  
    9. // Use this for initialization
    10. void Start () {
    11.  
    12. }
    13.  
    14. // Update is called once per frame
    15. void Update () {
    16.  
    17. }
    18. }
    19.  
    And the one that calls Ball :

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class planepilot : MonoBehaviour {
    7.  
    8.  public Ball ball;
    9.  
    10.  private string speedtext = "6" ;
    11.  public int ATCspeed = 6;
    12.  public int newSpeed ;
    13.  private string headingtext = "300" ;
    14.  public int ATCheading = 300;  
    15.  public int newHeading = 300;
    16.  public int heading ;
    17.  public float leftHeadingToTurn;
    18.  public bool turncomplete  ;
    19.  public bool past360 = false;
    20.  public bool headingAngleToTurnIsSet = false;
    21.  public float bankAngle = 0.0f ;
    22.  public Transform turnabout;
    23.  public float headingAngleToTurn;
    24.  public int previousHeading = 300;
    25.  
    26.  public float bankAngleSpeed;
    27.  public float noOfturndegreescomplete;
    28.  public float tempDegreesComplete;
    29.  void Start () {
    30.  previousHeading = 300;
    31.  ATCheading = 300;
    32.  
    33.  }
    34.  
    35.  
    36.  

    The 2nd code isn't complete as I didn't think it was necessary but I can post it if it helps. The reference to ball that you see in line 8 is the only reference to it in the entire script at the moment.
     
  7. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    What is the Ball class filename?
     
  8. Dazz500

    Dazz500

    Joined:
    Nov 13, 2014
    Posts:
    64
    Ball

    Ok now I've copied the contents of planepilot ( which I've partly listed above ) to another script, I've attached it to the same gameobject as planepilot and I DONT get the " error cs0103: The name 'Ball' does not exist in the current context ". So this is very strange but I guess I can just use this script instead.
     
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Maybe there was a weird character in there, or one of the L's (ell) was a 1 (one). Anyway, glad it's working now!
     
    Dazz500 likes this.