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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to pass gameObject type as custom type variable

Discussion in 'Scripting' started by kittik, Dec 4, 2015.

  1. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Hi,

    I am trying to pass a gameObject type, as a variable in a different script called checkpoint.

    The gameObject type cannot pass into a custom variable at present, however in the Inspector I can add the gameObject into the variable, giving me the impression that it must be possible.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LevelManager : MonoBehaviour {
    5.  
    6.     public Checkpoint checkpoint;
    7.     public GameObject currentCheckpoint;
    8.     private CharHealth charHealth;
    9.     private GameObject gamer;
    10.     public GameObject spellSpawn;
    11.  
    12.     void Start ()
    13.     {
    14.         gamer = GameObject.FindGameObjectWithTag("Player");  
    15.         charHealth = GameObject.FindGameObjectWithTag("Player").GetComponent<CharHealth>();
    16.     }
    17.    
    18.     public void RespawnPlayer()
    19.     {
    20.         string someString = checkpoint.checkpointsLevel.ToString();
    21.         Application.LoadLevel(someString);
    22.     }
    23. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Checkpoint : MonoBehaviour {
    5.  
    6.     public string checkpointsLevel;
    7.     public LevelManager levelManager;
    8.     private bool notDone;
    9.  
    10.     void Start ()
    11.     {
    12.         levelManager = FindObjectOfType<LevelManager>();
    13.         notDone = true;
    14.     }
    15.  
    16.     void OnTriggerEnter (Collider other)
    17.     {
    18.         if(other.name == "121115" && notDone == true)
    19.         {
    20.             levelManager.currentCheckpoint = gameObject;
    21.             notDone = false;
    22.         }
    23.     }
    24. }
    I am using public GameObject currentCheckpoint to pass the object into LevelManager in some way, it would be really good if I were able just to pass the GameObject into the Checkpoint type (within LevelManagers constructors), but I am not sure how to achieve this.

    I also thought that by creating public GameObject currentCheckpoint to pass the object in, it would be good to then automatically pass the object into the Checkpoint type.

    I had thought that by using Update within LevelManager, I could pass the gameObject into type Checkpoint, but cannot.

    Code (CSharp):
    1.  
    2. //To put in LevelManager.cs
    3. void Update ()
    4.     {
    5.         if(currentCheckpoint != null)
    6.         {
    7.             checkpoint = currentCheckpoint;
    8.             currentCheckpoint = null;
    9.         }
    10.     }
    I'm not sure at all on how to go about turning type GameObject into type Checkpoint. Any help would really be appreciated.
     
  2. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    This is one of the very basics of unity. a GameObject is an object which holds some components (= MonoBehaviours). the GameObject itself is not all its components but knows which components belongs to it.
    what you need is myGameObject.GetComponent<Checkpoint>();
     
  3. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Thank you @sz Bit Barons, I have tried this since posting the original message. Now I am facing another common problem.
    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if(currentCheckpoint != null)
    4.         {
    5.             checkpoint = currentCheckpoint.GetComponent<Checkpoint>().checkpointsLevel;
    6.             currentCheckpoint = null;
    7.         }
    8.     }
    The error being thrown is that it cannot convert string to type checkpoint, which is understandable. I'm looking at how to sort this out now.
     
    Last edited: Dec 4, 2015
  4. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Just attempted
    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if(currentCheckpoint != null)
    4.         {
    5.             checkpoint = currentCheckpoint.GetComponent<Checkpoint>().checkpointsLevel as Checkpoint;
    6.             currentCheckpoint = null;
    7.         }
    8.     }
    Still no luck. Console is giving error CS0039: Cannot convert type 'string' to 'Checkpoint' via a built-in conversion.
     
  5. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
    Remove the ".checkpointsLevel".
     
    kittik likes this.
  6. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Thank you, everything works as intended now.