Search Unity

Preventing use class variables

Discussion in 'Scripting' started by TheSniperFan, Jul 18, 2013.

  1. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Hello.

    I'm completely new to Unity and thus need some help because I have a problem I cannot solve the way I'd usually do.

    Before I start working on the project I have in mind, I need to get some experience with Unity so I do smaller stuff (at the moment I do pong) to get used with Unity. So far I have to say that Unity has a unique, very comfortable workflow, but there's one thing I cannot do the way I wanted to.

    The problem I have is that I need to manipulate/access properties of different GameObjects.
    Usually I would write a game or a logic-Object, create a new instance and call some sort of start()-Method from withing the main-Method. This logic-method would then manage everything (or most of the stuff) and if I needed to access a variable from another part of the script, I could just use base.getBLAH().
    The problem is that in Unity, when you attach a script to a GameObject, it's isolated since there is no main-Method.

    I worked around the problem by turning the variables I need into public class variables. While this is the most convenient and performance-efficient way, it also has a major drawback. The variable shared across all instances and thus totally useless when I create anything more complex than pong.

    So far I've read about SendMessage() and GetComponent(), but I don't quite understand how to use them yet. I would appreciate it if someone would help me at one part of my code.

    Here's my specific problem:
    I'm writing pong and have a "Ball" component. I need to access the position of the Ball from within the "Player" GameObject (and other places too, but this one should do for me to understand).

    Ball.cs:
    Code (csharp):
    1. public class Ball : MonoBehaviour {
    2.    
    3.     [...]
    4.     public static Vector3 pos;
    5.     [...]
    6.    
    7.     // Update is called once per frame
    8.     void Update () {
    9.         [...]
    10.         pos = mytransform.position;
    11.     }
    Player.cs
    Code (csharp):
    1. public class Player : MonoBehaviour {
    2.  
    3.     [...]
    4.  
    5.     void OnCollisionEnter(Collision collision)
    6.     {
    7.         if(mytransform.position.z > Ball.pos.z) {
    8.             [...]
    9.         }
    10.  
    11.         [...]
    12.  
    13.     }
    14. }
    You should get the idea.
    I also need to access other stuff from within different classes, but I think that if someone shows/explains me how to solve this problem here, I'll be able to do the rest.

    Thanks in advance.
     
  2. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
  3. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    *facepalm*

    I already saw that page and tried to apply it but failed.
    Turned out I confused GameObject.Find() with GetComponent(). I used the latter where I should have used the former and always got a Null-Pointer exception (or the Unity equivalent).

    Player.cs
    Code (csharp):
    1. public class Player : MonoBehaviour {
    2.    
    3.     private Transform mytransform;
    4.     private Transform balltransform;
    5.     [...]
    6.    
    7.     void Awake () {
    8.         mytransform = transform;
    9.         balltransform = GameObject.Find("Ball").transform;
    10.         [...]
    11.     }
    12.  
    13.     [...]
    Works perfectly fine.

    Thanks anyway! :D
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Move the Find call to Start to ensure that Ball is properly initialized before you find it.
     
  5. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Thanks for the hint.

    So if I understand it correctly Awake() is executed before Start() and thus I can use them like this:

    Awake: Do stuff with this Object.
    Start: Do stuff with other Objects (Initialization for example).

    Is this correct?
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Exactly correct. Anything called in Start can be called with the confidence that the scene is fully initialized.