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. Dismiss Notice

ERROR CODE: Cannot convert type `UnityEngine.Transform' to `UnityEngine.GameObject' via a built-in

Discussion in '2D' started by Ashwin13, Nov 19, 2018.

  1. Ashwin13

    Ashwin13

    Joined:
    Nov 13, 2018
    Posts:
    1
    hi all, I am following Brackeys tutorial on how to create a 2D platformer game in unity. I am using this for my project at school and going to give credit to the man himself. However the tutorial is a few years old so when I use the code he provided I get the error come up, it was in the title thread. Please, could I have help I have been stuck on this for a long time and there are no answers anywhere?

    ERROR CODE: Assets/Scripts/GameMaster.cs(55,106): error CS0039: Cannot convert type `UnityEngine.Transform' to `UnityEngine.GameObject' via a built-in conversion

    Game Master Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class GameMaster : MonoBehaviour
    6. {
    7.  
    8.     public static GameMaster gm;
    9.  
    10.     void Awake()
    11.     {
    12.         if (gm == null)
    13.         {
    14.             gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
    15.         }
    16.     }
    17.  
    18.     public Transform playerPrefab;
    19.     public Transform spawnPoint;
    20.     public float spawnDelay = 2;
    21.     public Transform spawnPrefab;
    22.  
    23.     public CameraShake cameraShake;
    24.  
    25.     void Start()
    26.     {
    27.         if (cameraShake == null)
    28.         {
    29.             Debug.LogError("No camera shake referenced in GameMaster");
    30.         }
    31.     }
    32.  
    33.     public IEnumerator _RespawnPlayer()
    34.     {
    35.         GetComponent<AudioSource>().Play();
    36.         yield return new WaitForSeconds(spawnDelay);
    37.  
    38.         Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
    39.         GameObject clone = Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation) as GameObject;
    40.         Destroy(clone, 3f);
    41.     }
    42.  
    43.     public static void KillPlayer(Player player)
    44.     {
    45.         Destroy(player.gameObject);
    46.         gm.StartCoroutine(gm._RespawnPlayer());
    47.     }
    48.  
    49.     public static void KillEnemy(Enemy enemy)
    50.     {
    51.         gm._KillEnemy(enemy);
    52.     }
    53.     public void _KillEnemy(Enemy _enemy)
    54.     {
    55.         GameObject _clone = Instantiate(_enemy.deathParticles, _enemy.transform.position, Quaternion.identity) as GameObject;
    56.         Destroy(_clone, 5f);
    57.         cameraShake.Shake(_enemy.shakeAmt, _enemy.shakeLength);
    58.         Destroy(_enemy.gameObject);
    59.     }
    60.  
    61. }
    62.  
    Enemy Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy : MonoBehaviour
    5. {
    6.  
    7.     [System.Serializable]
    8.     public class EnemyStats
    9.     {
    10.         public int maxHealth = 100;
    11.  
    12.         private int _curHealth;
    13.         public int curHealth
    14.         {
    15.             get { return _curHealth; }
    16.             set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }
    17.         }
    18.  
    19.         public int damage = 40;
    20.  
    21.         public void Init()
    22.         {
    23.             curHealth = maxHealth;
    24.         }
    25.     }
    26.  
    27.     public EnemyStats stats = new EnemyStats();
    28.  
    29.     public Transform deathParticles;
    30.  
    31.     public float shakeAmt = 0.1f;
    32.     public float shakeLength = 0.1f;
    33.  
    34.     [Header("Optional: ")]
    35.     [SerializeField]
    36.     private StatusIndicator statusIndicator;
    37.  
    38.     void Start()
    39.     {
    40.         stats.Init();
    41.  
    42.         if (statusIndicator != null)
    43.         {
    44.             statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
    45.         }
    46.  
    47.         if (deathParticles == null)
    48.         {
    49.             Debug.LogError("No death particles referenced on Enemy");
    50.         }
    51.     }
    52.  
    53.     public void DamageEnemy(int damage)
    54.     {
    55.         stats.curHealth -= damage;
    56.         if (stats.curHealth <= 0)
    57.         {
    58.             GameMaster.KillEnemy(this);
    59.         }
    60.  
    61.         if (statusIndicator != null)
    62.         {
    63.             statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
    64.         }
    65.     }
    66.  
    67.     void OnCollisionEnter2D(Collision2D _colInfo)
    68.     {
    69.         Player _player = _colInfo.collider.GetComponent<Player>();
    70.         if (_player != null)
    71.         {
    72.             _player.DamagePlayer(stats.damage);
    73.             DamageEnemy(9999999);
    74.         }
    75.     }
    76. }
     
  2. oLDo

    oLDo

    Joined:
    Mar 14, 2017
    Posts:
    55
    You are trying to instantiate Transform? It doesn't make sense. Instantiation is a creating copy of some Object. I think, you should have deathParticles type GameObject.