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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

GameObject disappears from inspector

Discussion in 'Scripting' started by CodeWurm, Oct 1, 2018.

  1. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    When I declare the variable in the start, it disappears from the inspector, why?
    Code (CSharp):
    1.  
    2. public class SpawnManager : MonoBehaviour {
    3.  
    4.     public GameObject Spawner;
    5.  
    6.     // Use this for initialization
    7.     public void Start () {
    8.         // Spawner = GetComponent<GameObject> ();
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.      
    15.     }
    16. }
    17.  
    18.  
    Here is where I get the error, at the 2th line of death method: Object reference not set to an instance of an object.
    Code (CSharp):
    1.  
    2. public class PlayerController : MonoBehaviour {
    3.  
    4.     public float jump;
    5.     public float speed;
    6.  
    7.     // Initializing the rigidbody attached
    8.     private Rigidbody rb;
    9.  
    10.     void Components()
    11.     {
    12.         // Declaring the rigidbody component
    13.         rb = GetComponent<Rigidbody> ();
    14.  
    15.     }
    16.  
    17.     void DeathPlayer()
    18.     {
    19.         if(rb.transform.position.y <= -1.5f)
    20.         {
    21.             SpawnManager spawn = gameObject.AddComponent<SpawnManager> () as SpawnManager;
    22.             rb.transform.position = spawn.Spawner.transform.position;
    23.             // Add timer from death to spawn point, to not lose all lives.
    24.             UIManager.lives -= 1;
    25.             print ("death");
    26.  
    27.         }
    28.     }
    29.     }
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I dont know if you can do getComponent on a GO

    You have the gameObject variable of the mb for that
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    The declaration of a variable is when you say the variable exists, like when you say
    public GameObject Spawner;


    The commented-out code you have in your Start function is not a declaration, it's the initialization, where you set the value of the variable.
    Spawner = GetComponent<GameObject> ();


    As SparrowsNest says, I don't think you can do GetComponent<GameObject>, because GameObject is not a component. But every MonoBehaviour automatically has a variable called "gameObject" that refers to the object it's attached to, so I suggest you use that instead.

    If the Spawner variable disappeared from the inspector when you added that line, my guess is that you got a compile error and didn't notice. Check your console.

    You are getting "Object reference not set to an instance of an object" because one of your variables has a value of null. Even if your initialization code in SpawnManager.Start() were working correctly (which I don't think it is), the variable "Spawner" would still be null at this point, because Start() hasn't run yet! Unity won't interrupt the function that's already running in order to call Start().

    But it looks like you don't actually need the Spawner variable it all. Instead of
    spawn.Spawner.transform.position
    , you can use
    spawn.gameObject.transform.position
    .

    Actually, you can just use
    spawn.transform.position
    and skip the GameObject entirely, because "transform" is accessible directly from any MonoBehaviour.

    If you were instantiating an object that actually needed to do its own setup before you continued, then I would suggest that you write your own function for doing that setup. Maybe something like:
    Code (CSharp):
    1. public class MyClass : MonoBehaviour
    2. {
    3.     bool initialized = false;
    4.     public void Init()
    5.     {
    6.         if (!initialized)
    7.         {
    8.             initialized = true;
    9.             // Do whatever you have to do to get the object ready to use here
    10.         }
    11.     }
    12. }
    Then, call Init() right after you instantiate the object.
     
  4. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    So much headache just to access a non-static variable.
     
  5. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    The code works fine I got no errors, even when players falls and dies. I still need to put the variable myself into the inspector because it keeps disappearing. Then I still get no errors, whats wrong?
     
  6. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    If the reference in the inspector is disappearing when you run the program, something in your code is clearing the reference, or possibly destroying the object. (Although the latter would probably show as 'missing' in the inspector rather than empty.

    Check your code for all references to the field and make sure nothing is trying to reassign it.
     
  7. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    In the spawnmanager class, I put the getcomponent in the start method, that's what made it disappear.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,186
    You can't
    GetComponent<GameObject>()
    .A GameObject isn't a component. It has components.

    If you want the GameObject the script is attached to, that's just
     .gameObject
     
  9. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    How do I write that down?
    Code (CSharp):
    1. Spawner = GetComponent<gameObject>();
    Keep getting errors:
    error CS0118: `UnityEngine.Component.gameObject' is a `property' but a `type' was expected
    error CS0584: Internal compiler error: The method or operation is not implemented.
    error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,186
    Code (csharp):
    1. Spawner = gameObject;
    But I don't think you understand what you're doing at all? Like, this stuff here:

    Code (csharp):
    1.  
    2. SpawnManager spawn = gameObject.AddComponent<SpawnManager> () as SpawnManager;
    3. rb.transform.position = spawn.Spawner.transform.position;
    You're adding a new spawn manager to the same object, and then expect that spawn manager to have a Spawner object you can move to. But you're not assigning that in any place. You're trying with:

    Code (csharp):
    1. Spawner = GetComponent<GameObject> ();
    But other than that not being valid code, you're just trying to get... some GameObject? The same gameobject? That would just be the player's gameobject.



    If you just want to have some marker Transform that defines where you respawn, you just set that directly as a Transform reference on your player script:

    Code (csharp):
    1. public class PlayerController : MonoBehaviour {
    2.     ...
    3.     public Transform respawnSpot;
    4.     ...
    5.  
    6.     void DeathPlayer()
    7.     {
    8.         if(rb.transform.position.y <= -1.5f)
    9.         {
    10.             rb.transform.position = respawnSpot.position;
    11.             ...
     
  11. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Ah thanks mate... I quess I think I know what I'm doing. Just a noob trying to make games.