Canot conert type: 'UnityEngine.Transform'

Hi unity team,

I am new here. I have upgraded the unity from 3.1 to 2017 and back again to 5.6. After I run the game, unity says ‘All compiler error has to be fixed before you can enter playmode.’

This code is incorrect and is displayed in the console section, Canot conert type:
‘UnityEngine.Transform’

Instantiate (Objectman,
transform.position +
new Vector3 (Random.Range (-radian, radians), 20, Random.Range (-radian, radians)),
Quaternion.identity);

Please, I do not know about this code.

Regards
Erick

We need more context for that code. My first guess is that “Objectman” may be a Transform (the function expects a GameObject), and you may be able to make it Objectman.gameObject to make it work.

But there’s not enough in that one line of code to actually solve this issue.

1 Like

This is the full code:
File name: Spawner.cs

/// <summary>
/// Spawner. this scripts just a spawner object.
/// </summary>
using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour
{
    private Transform Objectman = null;// object to spawn
    private float timeSpawn = 0;
    private int timeSpawnMax = 0;
    private float enemyCount = 0;
    public int radian = 0;

    private void Start ()
    {
        if (GetComponent<Renderer>())
            GetComponent<Renderer>().enabled = false;

    }

    private void Update ()
    {
        // find the spawned objects
        GameObject[] gos = GameObject.FindGameObjectsWithTag (Objectman.tag);
        timeSpawn += 1;
        if (gos.Length < enemyCount) {
            int timespawnmax = timeSpawnMax;
            if (timespawnmax <= 0) {
                timespawnmax = 10;
            }
            if (timeSpawn >= timespawnmax) {
                GameObject enemyCreated =
                    (GameObject)
                    Instantiate (Objectman,
                                transform.position +
                                new Vector3 (Random.Range (-radian, radian), 20, Random.Range (-radian, radian)),
                                Quaternion.identity);

                enemyCreated.transform.localScale = new Vector3 (Random.Range (5, 20), enemyCreated.transform.localScale.x,
                                                                enemyCreated.transform.localScale.x);

                timeSpawn = 0;

            }
        }

    }

}

can not be compiled too:
3203123--244939--Untitled-2.png

As @StarManta said, you need Objectman.gameObject in your Instantiate call.

However, that isn’t the only issue. Objectman is a private variable that you set = to null. I don’t see you ever assigning anything else to it, which means you’ll get null errors when you try to run it.

Before making an educated guess at your desired outcome, could you explain the goals of your script?

As @Brathnann points out, that’s not your only issue. I think there are a few other ‘helpful’ tips that could be passed along, too, in addition to the null(s).

Instantiate takes Object, not GameObject. Instantiating a Transform, or pretty much anything, is fine, although there are a few rules about instantiating stuff, which the docs cover. You can’t try to instantiate a Transform as a GameObject though, hence the compile error.

–Eric

This too.

This may be related to the above problem.

warning CS0618: UnityEngine.Application.loadedLevelName' is obsolete: Use SceneManager to determine what scenes have been loaded’

I have a red mark on the obsolete code:
Application.LoadLevel (Application.loadedLevelName); // obsolete <-----------------

using UnityEngine;
using System.Collections;

public class GameUI : MonoBehaviour
{

    public GUISkin skin;
    public Texture2D Logo;
    public int Mode;
    private GameManager game;
    private PlayerController play;
    private WeaponController weapon;
         
    void Start ()
    {
        game = (GameManager)GameObject.FindObjectOfType (typeof(GameManager));
        play = (PlayerController)GameObject.FindObjectOfType (typeof(PlayerController));
        weapon = play.GetComponent<WeaponController> ();
        // define player
     
    }

    public void OnGUI ()
    {
     
        if (skin)
            GUI.skin = skin;
     
     
        switch (Mode) {
        case 0:
            if (Input.GetKeyDown (KeyCode.Escape)) {
                Mode = 2; 
            }
         
            if (play) {
             
                play.Active = true;
         
                GUI.skin.label.alignment = TextAnchor.UpperLeft;
                GUI.skin.label.fontSize = 30;
                GUI.Label (new Rect (20, 20, 200, 50), "Kills " + game.Killed.ToString ());
                GUI.Label (new Rect (20, 60, 200, 50), "Score " + game.Score.ToString ());
             
                GUI.skin.label.alignment = TextAnchor.UpperRight;
                GUI.Label (new Rect (Screen.width - 220, 20, 200, 50), "ARMOR " + play.GetComponent<DamageManager> ().HP);
                GUI.skin.label.fontSize = 16;
             
                // Draw Weapon system
                //if (weapon != null && weapon.WeaponLists.Length > 0 && weapon.WeaponLists.Length < weapon.CurrentWeapon && weapon.WeaponLists [weapon.CurrentWeapon] != null) {
                    if (weapon.WeaponLists [weapon.CurrentWeapon].Icon)
                        GUI.DrawTexture (new Rect (Screen.width - 100, Screen.height - 100, 80, 80), weapon.WeaponLists [weapon.CurrentWeapon].Icon);
             
                    GUI.skin.label.alignment = TextAnchor.UpperRight;
                    if (weapon.WeaponLists [weapon.CurrentWeapon].Ammo <= 0 && weapon.WeaponLists [weapon.CurrentWeapon].ReloadingProcess > 0) {
                        if (!weapon.WeaponLists [weapon.CurrentWeapon].InfinityAmmo)
                            GUI.Label (new Rect (Screen.width - 230, Screen.height - 120, 200, 30), "Reloading " + Mathf.Floor ((1 - weapon.WeaponLists [weapon.CurrentWeapon].ReloadingProcess) * 100) + "%");
                    } else {
                        if (!weapon.WeaponLists [weapon.CurrentWeapon].InfinityAmmo)
                            GUI.Label (new Rect (Screen.width - 230, Screen.height - 120, 200, 30), weapon.WeaponLists [weapon.CurrentWeapon].Ammo.ToString ());
                    }
                //}else{
                    //weapon = play.GetComponent<WeaponController> ();
                //}
             
                GUI.skin.label.alignment = TextAnchor.UpperLeft;
                GUI.Label (new Rect (20, Screen.height - 50, 250, 30), "R Mouse : Switch Guns C : Change Camera");
         
            }else{
                play = (PlayerController)GameObject.FindObjectOfType (typeof(PlayerController));
                weapon = play.GetComponent<WeaponController> ();
            }
            break;
        case 1:
            if (play)
                play.Active = false;
         
            MouseLock.MouseLocked = false;
         
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            GUI.Label (new Rect (0, Screen.height / 2 + 10, Screen.width, 30), "Game Over");
     
            GUI.DrawTexture (new Rect (Screen.width / 2 - Logo.width / 2, Screen.height / 2 - 200, Logo.width, Logo.height), Logo);
     
            if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 + 50, 300, 40), "Restart")) {
 Application.LoadLevel (Application.loadedLevelName); // obsolete <-----------------    
            }
//            if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 + 100, 300, 40), "Main menu")) {
//                Application.LoadLevel ("Mainmenu");
//            }
            break;
     
        case 2:
            if (play)
                play.Active = false;
         
            MouseLock.MouseLocked = false;
            Time.timeScale = 0;
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            GUI.Label (new Rect (0, Screen.height / 2 + 10, Screen.width, 30), "Pause");
     
            GUI.DrawTexture (new Rect (Screen.width / 2 - Logo.width / 2, Screen.height / 2 - 200, Logo.width, Logo.height), Logo);
     
            if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 + 50, 300, 40), "Resume")) {
                Mode = 0;
                Time.timeScale = 1;
            }
//        if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 + 100, 300, 40), "Main menu")) {
//                Time.timeScale = 1;
//                Mode = 0;
//                Application.LoadLevel ("Mainmenu");
//            }
            break;
         
        }
     
    }
}

You also have a suggestion on how to proceed. Try googling the Unity docs for SceneManager and finding the/a suitable replacement? :slight_smile:

What exactly happened?
Someone can give a solution to this obsolete code. I really do not know about this code.

Application.LoadLevel (Application.loadedLevelName);

Obsolete message always tell you what to upgrade to…
Use the new SceneManager.