Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  3. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

SerializeField not working,variables are not shown in the inspector

Discussion in 'Scripting' started by maisaasaa, Dec 8, 2018 at 9:32 AM.

  1. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    27
    the aster script has variables that should be public in the inspector but this is not happening I don't get why
    there is also an error when I add gameinitializer script to the main camera,
    "the script needs to derive from monobehaviour"

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class aster : MonoBehaviour {
    6.     [SerializeField]
    7.     GameObject asteroid;
    8.     [SerializeField]
    9.     Sprite asteroid1;
    10.     [SerializeField]
    11.     Sprite asteroid2;
    12.     [SerializeField]
    13.     Sprite asteroid3;
    14.  
    15.     private CircleCollider2D circle;
    16.     // Use this for initialization
    17.     void Start()
    18.     {
    19.         random();
    20.  
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         // apply impulse force to get game object moving
    27.         const float MinImpulseForce = 3f;
    28.         const float MaxImpulseForce = 5f;
    29.         float angle = Random.Range(0, 2 * Mathf.PI);
    30.         Vector2 direction = new Vector2(
    31.             Mathf.Cos(angle), Mathf.Sin(angle));
    32.         float magnitude = Random.Range(MinImpulseForce, MaxImpulseForce);
    33.         GetComponent<Rigidbody2D>().AddForce(
    34.             direction * magnitude,
    35.             ForceMode2D.Impulse);
    36.     }
    37.     void OnBecameInvisible()
    38.     {
    39.         circle = asteroid1.GetComponent<CircleCollider2D>();
    40.         float shipRadius = circle.radius;
    41.         Vector3 shipPosition = transform.position;
    42.         Vector2 newPosition = new Vector2();
    43.         if (shipPosition.x + shipRadius > ScreenUtils.ScreenRight)
    44.         {
    45.             newPosition.x = ScreenUtils.ScreenLeft;
    46.             newPosition.y = shipPosition.y;
    47.         }
    48.         if (shipPosition.x - shipRadius < ScreenUtils.ScreenLeft)
    49.         {
    50.             newPosition.x = ScreenUtils.ScreenRight;
    51.             newPosition.y = shipPosition.y;
    52.         }
    53.         if (shipPosition.y + shipRadius > ScreenUtils.ScreenTop)
    54.         {
    55.             newPosition.x = shipPosition.x;
    56.             newPosition.y = ScreenUtils.ScreenBottom;
    57.         }
    58.         if (shipPosition.y - shipRadius < ScreenUtils.ScreenBottom)
    59.         {
    60.             newPosition.x = shipPosition.x;
    61.             newPosition.y = ScreenUtils.ScreenTop;
    62.         }
    63.         if (shipPosition.x + shipRadius > ScreenUtils.ScreenRight && shipPosition.y + shipRadius > ScreenUtils.ScreenTop)
    64.         {
    65.             newPosition.x = ScreenUtils.ScreenLeft;
    66.             newPosition.y = ScreenUtils.ScreenBottom;
    67.         }
    68.         if ((shipPosition.x - shipRadius < ScreenUtils.ScreenLeft && shipPosition.y + shipRadius > ScreenUtils.ScreenTop))
    69.         {
    70.             newPosition.x = ScreenUtils.ScreenRight;
    71.             newPosition.y = ScreenUtils.ScreenBottom;
    72.         }
    73.         if (shipPosition.x + shipRadius > ScreenUtils.ScreenRight && shipPosition.y - shipRadius < ScreenUtils.ScreenBottom)
    74.         {
    75.             newPosition.x = ScreenUtils.ScreenLeft;
    76.             newPosition.y = ScreenUtils.ScreenTop;
    77.         }
    78.         if (shipPosition.x - shipRadius < ScreenUtils.ScreenLeft && shipPosition.y - shipRadius < ScreenUtils.ScreenBottom)
    79.         {
    80.             newPosition.x = ScreenUtils.ScreenRight;
    81.             newPosition.y = ScreenUtils.ScreenTop;
    82.         }
    83.         transform.position = newPosition;
    84.     }
    85.  
    86.     void random()
    87.     {
    88.         //change sprite
    89.         SpriteRenderer spriterenderer = asteroid.GetComponent<SpriteRenderer>();
    90.         int SpriteNumber = Random.Range(0, 3);
    91.         if (SpriteNumber < 1)
    92.         {
    93.             spriterenderer.sprite = asteroid1;
    94.         }
    95.         else if (SpriteNumber < 2)
    96.         {
    97.             spriterenderer.sprite = asteroid2;
    98.         }
    99.         else
    100.         {
    101.             spriterenderer.sprite = asteroid3;
    102.         }
    103.     }
    104.  
    105. }
    106.  
     
  2. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    189
    You have to fix your error first, read the console log.
     
  3. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    27
    I don't understand it,and that piece of code used to work before but in this project the inspector doesn't show the variables
     
  4. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    27
    this is the error:

    Assets/aster.cs(39,28): error CS1061: Type `UnityEngine.Sprite' does not contain a definition for `GetComponent' and no extension method `GetComponent' of type `UnityEngine.Sprite' could be found. Are you missing an assembly reference?
     
  5. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    189
    The error is pretty clear, the type "Sprite" doesn't have a method "GetComponent" but in the line 39 you have the following code:
    circle = asteroid1.GetComponent<CircleCollider2D>();

    You declared "asteroid1" with the type "Sprite" that's why you get the error. But I don't know what you try to do here, why do you want the collider of the sprite? A sprite is just a "image" it doesn't have a collider. Maybe you have a typo somewhere and wanted to use a different variable instead of asteroid1?
     
  6. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    27
    I attached a collider to the sprite because I need it in the screenwrap code, I compare (the radius+point x of sprite position) with the screen right value then change the location to the left(this in case the ship or rock are going right and I need them to appear from left)
    simply the error is telling me that there is no gameobject attached to the script which is true because SerializeField is not working so I am not able to attach the sprite to the script
    it needs to be fixed first and the variables appear in the inspector then attach them to the sprites and then apply the code to get the radius of the collider
     
  7. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    189
    Okay maybe im missing something here, but how did you add the collider directly to the sprite?
    I didn't know that you can add a collider directly to a sprite, because I usually add them to the GameObject in the scene/prefab.

    But GetComponent can't be used for sprites, because they aren't a "GameObject", maybe Sprites have a different method to get their collider or maybe we are missing something else.
     
    Suddoha likes this.
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,160
    No, that's not what the error says.
    And GameObjects cannot be attached to "scripts", but "scripts" a.k.a components can be attached to GameObjects.

    The error has already been explained. You're trying to call a method (GetComponent) on an instance (asteroid1) of a type (Sprite), but that type has no definition for it. Neither does an extension exist.
    Sprite is not even a component, that's why it does not have that method.

    Just make another field for the SpriteRenderer and use that one to get the collider.
     
  9. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    27
    that piece of code used to work in a previous project, but the problem is I can't get the game object to be attached to the script, as long as [SerializeField] doesn't work and the variables are not showing in the inspector I don't know how I can attach them
     
  10. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    27
    I already have a gameobject field in the code, the problem is to connect the script to the gameobject so we can "GetComponent" of it
     
  11. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    189
    But you are not using the GameObject field for the "GetComponent" call, you are using the Sprite field.
    Again, at line 39:
    circle = asteroid1.GetComponent<CircleCollider2D>();

    Your fields are:
    [SerializeField]
    GameObject asteroid;
    [SerializeField]
    Sprite asteroid1;

    Maybe there is just a typo in the line 39 and you want to use the "asteroid" (GameObject) field instead.
     
    maisaasaa and Suddoha like this.
  12. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,160
    You have to fix the error first. That's why it does not show up. The script cannot be compiled, thus Unity will refuse to update the inspectors and especially the instances of the 'aster' type.

    You're facing a compile-time error here.
    What you're talking about would be a runtime error, which is not yet the case.
     
  13. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    27
    well I fixed that error by changing the name in the inspector to just "asteroid" and now the variables show up but that doesn't happen in the ship.cs although I used the same code and there are no typos

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ships : MonoBehaviour
    6. {  
    7.     public GameObject ship;
    8.    
    9.     public Sprite ship1;
    10.    
    11.     public Sprite ship2;
    12.    
    13.     public Sprite ship3;
    14.  
    15.     private CircleCollider2D circle;
    16.     private Rigidbody2D RB;
    17.      int Thrustforce = 5;
    18.    
    19.     int rotateDegreesPerSecond = 5;
    20.      float rad;
    21.     Vector2 thrustDirection = new Vector2(1, 0);
    22.     void Start()
    23.     {
    24.         random();
    25.     }
    26.     void Update()
    27.     { //apply thrust with space key
    28.         float horizontalInput = Input.GetAxis("Thrust");
    29.         if (horizontalInput > 0)
    30.         {
    31.             Vector3 newShipAngle = transform.eulerAngles;
    32.             thrustDirection.x = Mathf.Cos(newShipAngle.z * Mathf.Deg2Rad);
    33.             thrustDirection.y = Mathf.Sin(newShipAngle.z * Mathf.Deg2Rad);
    34.  
    35.             RB = GetComponent<Rigidbody2D>();
    36.             RB.AddForce(Thrustforce * thrustDirection, ForceMode2D.Force);
    37.         }
    38.         //apply rotation with left and right
    39.         float rotationInput = Input.GetAxis("Rotate");
    40.         if (rotationInput > 0)
    41.         {
    42.             // calculate rotation amount and apply rotation
    43.             float rotationAmount = rotateDegreesPerSecond * Time.deltaTime;
    44.             if (rotationInput < 0)
    45.             {
    46.                 rotationAmount *= -1;
    47.             }
    48.             transform.Rotate(Vector3.forward, rotationAmount);
    49.         }
    50.  
    51.     }
    52.     void random()
    53.     {//change sprite
    54.         SpriteRenderer spriterenderer =ship.GetComponent<SpriteRenderer>();
    55.         int SpriteNumber = Random.Range(0, 3);
    56.         if (SpriteNumber < 1)
    57.         {
    58.             spriterenderer.sprite = ship1;
    59.         }
    60.         else if (SpriteNumber < 2)
    61.         {
    62.             spriterenderer.sprite = ship2;
    63.         }
    64.         else
    65.         {
    66.             spriterenderer.sprite = ship3;
    67.         }
    68.     }
    69.     //screenwrap
    70.     void OnBecameInvisible()
    71.     {
    72.         circle =ship.GetComponent<CircleCollider2D>();
    73.         float shipRadius = circle.radius;
    74.         Vector3 shipPosition = transform.position;
    75.         Vector2 newPosition = new Vector2();
    76.         if (shipPosition.x + shipRadius > ScreenUtils.ScreenRight)
    77.         {
    78.             newPosition.x = ScreenUtils.ScreenLeft;
    79.             newPosition.y = shipPosition.y;
    80.         }
    81.         if (shipPosition.x - shipRadius < ScreenUtils.ScreenLeft)
    82.         {
    83.             newPosition.x = ScreenUtils.ScreenRight;
    84.             newPosition.y = shipPosition.y;
    85.         }
    86.         if (shipPosition.y + shipRadius > ScreenUtils.ScreenTop)
    87.         {
    88.             newPosition.x = shipPosition.x;
    89.             newPosition.y = ScreenUtils.ScreenBottom;
    90.         }
    91.         if (shipPosition.y - shipRadius < ScreenUtils.ScreenBottom)
    92.         {
    93.             newPosition.x = shipPosition.x;
    94.             newPosition.y = ScreenUtils.ScreenTop;
    95.         }
    96.         if (shipPosition.x + shipRadius > ScreenUtils.ScreenRight && shipPosition.y + shipRadius > ScreenUtils.ScreenTop)
    97.         {
    98.             newPosition.x = ScreenUtils.ScreenLeft;
    99.             newPosition.y = ScreenUtils.ScreenBottom;
    100.         }
    101.         if ((shipPosition.x - shipRadius < ScreenUtils.ScreenLeft && shipPosition.y + shipRadius > ScreenUtils.ScreenTop))
    102.         {
    103.             newPosition.x = ScreenUtils.ScreenRight;
    104.             newPosition.y = ScreenUtils.ScreenBottom;
    105.         }
    106.         if (shipPosition.x + shipRadius > ScreenUtils.ScreenRight && shipPosition.y - shipRadius < ScreenUtils.ScreenBottom)
    107.         {
    108.             newPosition.x = ScreenUtils.ScreenLeft;
    109.             newPosition.y = ScreenUtils.ScreenTop;
    110.         }
    111.         if (shipPosition.x - shipRadius < ScreenUtils.ScreenLeft && shipPosition.y - shipRadius < ScreenUtils.ScreenBottom)
    112.         {
    113.             newPosition.x = ScreenUtils.ScreenRight;
    114.             newPosition.y = ScreenUtils.ScreenTop;
    115.         }
    116.         transform.position = newPosition;
    117.     }
    118.  
    119. }
    120.  
     
  14. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    27
    I fixed that but now same error in ship.cs but the name is right in both the inspector and the code
     
  15. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,160
    Please, always include the error from the console. Most of the time it includes detailed information such as line numbers and those would be really helpful. It's better than having to read the complete script.
     
  16. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    27
    that is all is shown
     
  17. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,160
    There is no error, even though the warning in the inspector window might suggest that.

    Make sure file name and class name match exactly (your class is named ships, the file's name is ship). Then try to re-attach the script, if it does not work, tell us what happens when you try it.
     
  18. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    27
    changing the names worked