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

Cannot implicitly convert type 'PlayerController' to 'UnityEngine.GameObject'

Discussion in 'Scripting' started by alperkicirli, Apr 16, 2020.

  1. alperkicirli

    alperkicirli

    Joined:
    Apr 15, 2020
    Posts:
    10
    Assets\Scripts\GameManager.cs(22,29): error CS0029: Cannot implicitly convert type 'PlayerController' to 'UnityEngine.GameObject'

    How could i fix this problem? Here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     public List<Character> Characters = new List<Character>();
    8.     public List<Item> AllItems = new List<Item>();
    9.     public Character CurrentCharacter;
    10.     bool ShowCharWheel;
    11.     public int SelectedCharacter;
    12.     int LastCharacter;
    13.     public static GameManager Instance;
    14.     public bool CanShowSwitch = true;
    15.     public LootChest SelectedChest;
    16.  
    17.     void Awake()
    18.     {
    19.         Instance = this;
    20.         foreach (Character c in Characters)
    21.         {
    22.            
    23.         GameObject go = Instantiate(c.PlayerPrefab, c.HomeSpawn.position, c.HomeSpawn.rotation) as GameObject;
    24.          c.Instance = go.GetComponent<PlayerController>();
    25.         }
    26.         ChangeCharacterStart(Characters[PlayerPrefs.GetInt("SelectedChar")]);
    27.     }
    28.  
    29.     // Start is called before the first frame update
    30.     void Start()
    31.     {
    32.    
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         if (Input.GetKey(KeyCode.C))
    39.         {
    40.             ShowCharWheel = true;
    41.             Time.timeScale = 0.5f;
    42.         }
    43.         else
    44.         {
    45.             ShowCharWheel = false;
    46.             Time.timeScale = 1;
    47.         }
    48.     }
    49.  
    50.     void ChangeCharacterStart(Character c)
    51.     {
    52.         LastCharacter = SelectedCharacter;
    53.         SelectedCharacter = Characters.IndexOf(c);
    54.         CurrentCharacter = c;
    55.         Characters[LastCharacter].Instance.GetComponent<PlayerController>().CanPlay = false;
    56.         Characters[SelectedCharacter].Instance.GetComponent<PlayerController>().CanPlay = true;
    57.         Camera.main.GetComponent<SmoothFollowCSharp>().target = Characters[SelectedCharacter].Instance.transform;//BuraDeaktifOlacak
    58.         PlayerPrefs.SetInt("SelectedChar", SelectedCharacter);
    59.     }
    60.  
    61.     void ChangeCharacter(Character c)
    62.     {
    63.         if (Vector3.Distance(Characters[SelectedCharacter].Instance.transform.position, c.Instance.transform.position) > 10)
    64.         {
    65.             SequenceManager.Instance.StartCoroutine("DoCharSwitch", c);
    66.             CanShowSwitch = false;
    67.             LastCharacter = SelectedCharacter;
    68.             SelectedCharacter = Characters.IndexOf(c);
    69.             CurrentCharacter = c;
    70.             Characters[LastCharacter].Instance.GetComponent<PlayerController>().CanPlay = false;
    71.             Characters[SelectedCharacter].Instance.GetComponent<PlayerController>().CanPlay = true;
    72.             PlayerPrefs.SetInt("SelectedChar", SelectedCharacter);
    73.         }
    74.         else
    75.         {
    76.             LastCharacter = SelectedCharacter;
    77.             SelectedCharacter = Characters.IndexOf(c);
    78.             CurrentCharacter = c;
    79.             Characters[LastCharacter].Instance.GetComponent<PlayerController>().CanPlay = false;
    80.             Characters[SelectedCharacter].Instance.GetComponent<PlayerController>().CanPlay = true;
    81.             PlayerPrefs.SetInt("SelectedChar", SelectedCharacter);
    82.             Camera.main.GetComponent<SmoothFollowCSharp>().target = Characters[SelectedCharacter].Instance.transform;
    83.         }
    84.  
    85.     }
    86.  
    87.     void OnGUI()
    88.     {
    89.         if (ShowCharWheel && CanShowSwitch)
    90.         {
    91.             GUILayout.BeginArea(new Rect(Screen.width - 64, Screen.height - 192, 64, 192),GUIContent.none, "box");
    92.             foreach (Character c in Characters)
    93.             {
    94.                 if (GUILayout.Button(c.Icon, GUILayout.Width(64),GUILayout.Height(64)))
    95.                 {
    96.                     ChangeCharacter(c);                
    97.                 }
    98.             }
    99.             GUILayout.EndArea();
    100.         }
    101.     }
    102. }
    103.  
    104. [System.Serializable]
    105. public class Character
    106. {
    107.     public string Name;
    108.     public Texture2D Icon;
    109.     public PlayerController PlayerPrefab;
    110.     public PlayerController Instance;
    111.     public Transform HomeSpawn;
    112. }
    113.  
    114. [System.Serializable]
    115. public class Item
    116. {
    117.     public string Name;
    118.     public Texture2D Icon;
    119.     public ItemInstance InstancePrefab;
    120. }
     
    Last edited: Apr 16, 2020
  2. alperkicirli

    alperkicirli

    Joined:
    Apr 15, 2020
    Posts:
    10
    Not fixed yet.
     
    Last edited: Apr 16, 2020
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Remove " as PlayerController" from the end of line 22.
     
  4. alperkicirli

    alperkicirli

    Joined:
    Apr 15, 2020
    Posts:
    10
    I changed. New code:
    GameObject go = Instantiate(c.PlayerPrefab, c.HomeSpawn.position, c.HomeSpawn.rotation) as GameObject;
    c.Instance = go.GetComponent<PlayerController>();

    New error :(
    Assets\Scripts\GameManager.cs(22,29): error CS0039: Cannot convert type 'PlayerController' to 'UnityEngine.GameObject' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    c.PlayerPrefab refers to a component on the GameObject. You need to instantiate the GameObject itself, so you need to pass in c.PlayerPrefab.gameObject to the Instantiate call.
     
    alperkicirli and PraetorBlue like this.
  6. alperkicirli

    alperkicirli

    Joined:
    Apr 15, 2020
    Posts:
    10
    FIXED. Thanks a lot :)
     
    Last edited: Apr 16, 2020