Search Unity

[Help] Can't add object reference to script

Discussion in 'Scripting' started by aribarzilai, Feb 17, 2018.

  1. aribarzilai

    aribarzilai

    Joined:
    Feb 17, 2018
    Posts:
    3
    Hi, I'm learning unity 2D by watching a step-by-step tutorial that's creating a simple Pong game. The tutorial is well-made but a bit outdated (back from when unity supported javascript still, so I coded the scripts on my own in c#).

    At this point in the video, however, he assigns the references by dragging the objects into the object references of the script. On my client, however, I receive the message "Default references will only be applied in edit mode." This sort of 'locks' the script and prevents me from assigning the reference.

    Here's the code I wrote for the script (based on what he wrote in javascript, I simply wrote it in c#). my understanding of unity is limited, but I assumed it was working fine even if inefficient.

    My code: (C#)

    Code (CSharp):
    1. public class GameSetup : MonoBehaviour {
    2.  
    3.     [SerializeField] Camera mainCamera;
    4.     [SerializeField] BoxCollider2D topWall;
    5.     [SerializeField] BoxCollider2D botWall;
    6.     [SerializeField] BoxCollider2D leftWall;
    7.     [SerializeField] BoxCollider2D rightWall;
    8.     [SerializeField] Transform player1;
    9.     [SerializeField] Transform player2;
    10.     // Update is called once per frame
    11.     void Update () {
    12.         topWall.size = new Vector2(mainCamera.ScreenToWorldPoint(new Vector3(Screen.width * 2f, 0f, 0f)).x, 1f);
    13.         topWall.offset = new Vector2(0f, mainCamera.ScreenToWorldPoint(new Vector3(0f, Screen.height, 0f)).y+0.5f);
    14.  
    15.         botWall.size = new Vector2(mainCamera.ScreenToWorldPoint(new Vector3(Screen.width * 2, 0f, 0f)).x, 1f);
    16.         botWall.offset = new Vector2(0f, mainCamera.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).y - 0.5f);
    17.  
    18.         leftWall.size = new Vector2(1f, mainCamera.ScreenToWorldPoint(new Vector3(0f, Screen.height * 2f, 0f)).y); ;
    19.         leftWall.offset = new Vector2(mainCamera.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - 0.5f, 0f);
    20.  
    21.         rightWall.size = new Vector2(1f, mainCamera.ScreenToWorldPoint(new Vector3(0f, Screen.height * 2f, 0f)).y);
    22.         rightWall.offset = new Vector2(mainCamera.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + 0.5f, 0f);
    23.     }
    24. }
    The original code I based it off of: (Javascript)

    Code (JavaScript):
    1. //Reference the camera
    2. var mainCam : Camera;
    3.  
    4. //Reference the colliders we are going to adjust
    5. var topWall : BoxCollider2D;
    6. var bottomWall : BoxCollider2D;
    7. var leftWall : BoxCollider2D;
    8. var rightWall : BoxCollider2D;
    9.  
    10. //Reference the players
    11. var Player01 : Transform;
    12. var Player02 : Transform;
    13.  
    14. function Start () { //Only set this to Update if you know the screen size can change during a playsession.
    15.  
    16.     //Move each wall to its edge location:
    17.     topWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2f, 0f, 0f)).x, 1f);
    18.     topWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 ( 0f, Screen.height, 0f)).y + 0.5f);
    19.  
    20.     bottomWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2, 0f, 0f)).x, 1f);
    21.     bottomWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3( 0f, 0f, 0f)).y - 0.5f);
    22.  
    23.     leftWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);;
    24.     leftWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - 0.5f, 0f);
    25.  
    26.     rightWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
    27.     rightWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + 0.5f, 0f);
    28.  
    29.     //Move the players to a fixed distance from the edges of the screen:
    30.     Player01.position.x = mainCam.ScreenToWorldPoint (new Vector3 (75f, 0f, 0f)).x;
    31.     Player02.position.x = mainCam.ScreenToWorldPoint (new Vector3 (Screen.width -75f, 0f, 0f)).x;
    32. }
    I figure that I mucked the conversion up somehow with the Serialized Field, but without that the field doesn't show up in the script. Any help would be appreciated
     
    Last edited: Feb 17, 2018
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Not sure if that's the problem, but I assume the SerializeField attribute is applied to the first field only, named topWall.

    You probably want to decorate each field with the SerializeField attribute, for example as shown below.
    Code (CSharp):
    1. [SerializeField] BoxCollider2D topWall;
    2. [SerializeField] BoxCollider2D botWall;
    3. [SerializeField] BoxCollider2D leftWall;
    4. [SerializeField] BoxCollider2D rightWall;
     
    aribarzilai likes this.
  3. aribarzilai

    aribarzilai

    Joined:
    Feb 17, 2018
    Posts:
    3
    Thanks for commenting! I don't think it changes anything because the semicolon (end statement) only appears at the end, but i fixed it (better safe then sorry, right?)

    new section of that code:
    Code (CSharp):
    1.     [SerializeField] Camera mainCamera;
    2.     [SerializeField] BoxCollider2D topWall;
    3.     [SerializeField] BoxCollider2D botWall;
    4.     [SerializeField] BoxCollider2D leftWall;
    5.     [SerializeField] BoxCollider2D rightWall;
    6.     [SerializeField] Transform player1;
    7.     [SerializeField] Transform player2;
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    Nope, it gets applied to all of them! But, there's a bug in Unity's compiler implementation where every field but the first one gets a "this field is never assigned to and will always be null" warning.
    But for general C# use, it's good to know that when you declare a bunch of fields in a row like what's done here, attributes gets applied to all of them.


    @azibarzilal, when you're dragging values onto a script file, you're setting which default values will be set when you put that script on an object in the editor. As the message is telling you, those defaults does not apply in play mode (or in builds), so it's not that usefull.
    In addition, there's a strict seperation between objects that are in a scene, and objects that are assets in the assets folder. I bet you're trying to drag a box collider 2D from the scene, onto the script asset. Assets in Unity cannot reference objects in a scene. The other way around is fine, though.


    What you want to do is to add the script to a GameObject in the scene, and click that object. That will have you editing the script instance that's in your scene rather than the script asset itself.
     
    triishac and Peter77 like this.
  5. aribarzilai

    aribarzilai

    Joined:
    Feb 17, 2018
    Posts:
    3
    Ahhhh, I see now! That makes perfect sense. Thank you! It works now, too :^)
     
    Last edited: Feb 17, 2018