Search Unity

Question Error CS1061: 'GameObject[]' does not contain a definition for 'transform'...

Discussion in '2D' started by baptistedigital, Oct 18, 2020.

  1. baptistedigital

    baptistedigital

    Joined:
    Jul 8, 2020
    Posts:
    4
    Bonjour tout le monde.

    Je sollicite votre aide pour m'éclairer sur un script. Je précise être un vrai débutant sur Unity et en C#.

    Contexte : Je souhaite developper un piège qui tire de boules de feu. Ces boules de feu doivent se diriger vers mon personnage au Tag "Player" et s'autodétruire au bout d'un temps que je déterminerai lors des tests.

    Probleme : Je ne parviens pas a developper un script qui dirige les boules de feu vers le Player

    Script actuel :

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class enemyShot : MonoBehaviour
    {
    public float speed = 7f;
    Transform myTransform;
    public Position target ;



    void Start()
    {
    myTransform = GetComponent<Transform>();
    target = GameObject.FindGameObjectsWithTag("Player").transform.position;


    }

    // Update is called once per frame
    void Update()
    {



    Vector3 dir = target.position - myTransform.position;
    myTransform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);

    }
    }


    Merci pour votre aide
    Baptiste
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    Hello @baptistedigital,
    First off, please note that our Unity forum is an English forum. Please ask your question in English, so that the most amount of people can both provide help, and learn from the question and answers.

    In your title, you put
    Error CS1061: 'GameObject[]' does not contain a definition for 'transform'
    . This error is most likely from the following line
    target = GameObject.FindGameObjectsWithTag("Player").transform.position;
    .
    The call
    GameObject.FindGameObjectsWithTag
    returns an array of GameObjects, however, you are treating it as a single GameObject. Try to switch it out to GameObject.FindWithTag which returns a single GameObject with the tag specified.
     
  3. baptistedigital

    baptistedigital

    Joined:
    Jul 8, 2020
    Posts:
    4
    Hello Ted, thank you for your answer and sorry for the langage mistake

    Here is an English version of the question :

    As a beginner in Unity and coding I have a lot of daily questions when I develop my project.
    In general I find a solution from myself or from the internet.

    But the following one remains a kind of mystery for me.

    Context : I want to develop a trap which is a tower. Fireballs will be instantiated from this one and they will be launched in the direction of the Player. But I do not success in establishing a system to drive the balls to the position of the player.

    Script :


    Code (CSharp):
    1. using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.  
    5.     public class enemyShot : MonoBehaviour
    6.     {
    7.         public float speed = 12f;
    8.         Transform myTransform;
    9.         public Position target ;
    10.    
    11.      
    12.         // Start is called before the first frame update
    13.         void Start()
    14.         {
    15.                 myTransform = GetComponent<Transform>();
    16.                 target = GameObject.FindGameObjectsWithTag("Player").transform.position;
    17.              
    18.          
    19.         }
    20.  
    21.         // Update is called once per frame
    22.         void Update()
    23.         {
    24.          
    25.  
    26.          
    27.             Vector3 dir = target.position - myTransform.position;
    28.            myTransform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
    29.  
    30.         }
    31.     }
    32.  
    33.  
    34.  
    35.  
    36. Thank you for your advices
    37.  
    38. Cheers
    39.  
    40.  
    41.  
    42.          
    43.             Vector3 dir = target.position - myTransform.position;
    44.            myTransform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
    45.  
    46.         }
    47.     }

    I noticed this difference and I tried to use a
    FindGameObjects.FindWithTag
    rather than
    GameObject.FindGameObjectsWithTag
    .
    Nevertheless it wrote me "error CS0246: The type or namespace name 'Position' could not be found (are you missing a using directive or an assembly reference?)"
    To that extent I believe that the issue comes from my variable declaration (
    Public Position Target;
    ). Do you share my idea or do you think there is something other which is wrong?

    Thank you all

    Baptiste
     
  4. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    Thanks for the updated post, it is a lot easier to parse it now.

    So by switching from
    FindGameObjectsWithTag
    to
    FindWithTag
    , we fixed one issue. You are correct about the next issue, that the compile error comes from
    public Position Target
    . On this line you are declaring a member variable of the type Position. In Unity's API, we do not have a data type called Position, and this is why you are seeing this compile error. Instead, you should switch
    Position
    to
    Vector3
    , which is the return type of transform.positon.

    At our Scripting Reference site, you can see the return type highlighted like this:


    Let me know how it goes!
     
  5. baptistedigital

    baptistedigital

    Joined:
    Jul 8, 2020
    Posts:
    4
    Thank you for your help Ted, I finally found a solution which display the expected results, here is the right script if someone needs it :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class enemyShot : MonoBehaviour
    7. {
    8.     public float speed = 12f;
    9.     Transform myTransform;
    10.  
    11.     public Transform target;
    12.     public Vector3 position;
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.             myTransform = GetComponent<Transform>();
    19.              target.position = GameObject.FindWithTag("Player").transform.position;
    20.          
    21.      
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.      
    28.  
    29.      
    30.         Vector3 dir = target.position - myTransform.position;
    31.        myTransform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
    32.  
    33.     }
    34. }
    35.  

    Have a nice day and see you soon :)

    Baptiste
     
    Ted_Wikman likes this.