Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Error CS0029. Can't get object transfom from tag

Discussion in 'Scripting' started by daano96, Oct 21, 2020.

Thread Status:
Not open for further replies.
  1. daano96

    daano96

    Joined:
    Mar 27, 2016
    Posts:
    2
    I am trying to let players spawn on an object. And the spawn point is an object with the tag "Green". But it says: error CS0029: Cannot implicitly convert type 'UnityEngine.GameObject[]' to 'UnityEngine.Transform'. I have no idea how to fix it. Thanks in Advance


    Code (CSharp):
    1. public class PlayerSpawner : MonoBehaviour
    2. {
    3.     public Transfrom[] spawnLoacations;
    4.     public GameObject[] playerPrefab;
    5.     public GameObject[] playerPrefabClone;
    6.  
    7.     bool green_player = true;
    8.  
    9.     void Start()
    10.     {
    11.         spawnLoacations[0] = GameObject.FindWithTag("Green").transform;
    12.  
    13.         if (green_player == true)
    14.         {
    15.             SpawnPlayer();
    16.         }
    17.  
    18.     }
    19.  
    20.     void SpawnPlayer()
    21.     {
    22.         playerPrefabClone[0] = Instantiate(playerPrefab[0], spawnLoacations[0].transform.position + new Vector3(0,0,-1), Quaternion.Euler(0, 0, 0)) as GameObject;
    23.     }
    24. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    Are you sure this is your actual code? Did you remember to save your file? I would expect this error if you were using
    GameObject.FindGameObjectsWithTag("Green")
    which returns an array instead of a single GameObject.
     
  3. daano96

    daano96

    Joined:
    Mar 27, 2016
    Posts:
    2
    I tried it at least a hundred times and it didn't work but the Transfrom[] must, of course, be Transform[]. Everything works now. Thank you for your help and time anyways.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    This is really wiggy-wonky code: Line 11 will always fail... UNLESS somebody drags at least one reference into the
    spawnLoacations
    (sic; misspelled) variable, at which point Line 11 will WIPE OUT that reference and replace it (possibly) with what it found.

    Usually you set stuff up in the editor, OR you Find it at runtime, rarely both the way this is doing.

    Good luck!
     
  5. drachoub09

    drachoub09

    Joined:
    Apr 28, 2022
    Posts:
    1
    Try using FindWithTag();
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    This was three years ago, please don't necropost.
     
Thread Status:
Not open for further replies.