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

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();
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

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.

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 :

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
 
    public class enemyShot : MonoBehaviour
    {
        public float speed = 12f;
        Transform myTransform;
        public Position target ;
   
     
        // Start is called before the first frame update
        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);
 
        }
    }
 



Thank you for your advices

Cheers


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

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

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!

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 :

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


public class enemyShot : MonoBehaviour
{
    public float speed = 12f;
    Transform myTransform;
  
    public Transform target;
    public Vector3 position;
 
  
    // Start is called before the first frame update
    void Start()
    {
            myTransform = GetComponent<Transform>();
             target.position = GameObject.FindWithTag("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);

    }
}

Have a nice day and see you soon :slight_smile:

Baptiste

1 Like