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. Dismiss Notice

'GameObject[]' does not contain a definition for 'GetComponent' and no accessible extension method '

Discussion in '2D' started by TheGamingMusketeers, Apr 8, 2020.

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

    TheGamingMusketeers

    Joined:
    Apr 4, 2020
    Posts:
    3
    I'm trying to make a script to make an AI follow me in a topdown game I'm making for a jam. but it keeps saying
    'GameObject[]' does not contain a definition for 'GetComponent' and no accessible extension method 'GetComponent' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?)
    here's the code


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class follow : MonoBehaviour
    6. {
    7.     public float speed;
    8.  
    9.     private Transform target;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         target = GameObject.FindGameObjectsWithTag("Player").GetComponent<Transform>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    20.     }
    21. }
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    FindGameObjectsWithTag returns an array. Attaching a method call directly to the end of it tries to call the method on the array itself and not the actual objects in the array. You need to use a loop to access the individual objects as shown in the first and second examples in the documentation.

    https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
     
  3. TheGamingMusketeers

    TheGamingMusketeers

    Joined:
    Apr 4, 2020
    Posts:
    3
    i
    I don't really understand can you give me a direct working code for an ai to follow the player
     
  4. TheGamingMusketeers

    TheGamingMusketeers

    Joined:
    Apr 4, 2020
    Posts:
    3
    oh I discovered I wasn't using the right visual studio code tools
     
  5. kcmnzynp

    kcmnzynp

    Joined:
    Nov 17, 2020
    Posts:
    2
    I'm trying to make a script to make an AI follow me in a topdown game I'm making for a jam. but it keeps saying
    'GameObject[]' does not contain a definition for Assets\Scripts\Resetter.cs(39,74): error CS1061: 'GameObject[]' does not contain a definition for 'GetComponent' and no accessible extension method 'GetComponent' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?)
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @kcmnzynp

    You should create your own thread, instead of reviving some old thread.
    And your question is not 2D related either.

    GameObject[] is an array of GameObjects.

    You need to get one GameObject from your array and then use GetComponent.

    Array doesn't have a GetComponent method, but Unity GameObject (or Component) does have a GetComponent method.
     
    Last edited: Dec 14, 2020
    kcmnzynp likes this.
  7. kcmnzynp

    kcmnzynp

    Joined:
    Nov 17, 2020
    Posts:
    2
    @eses
    I don't really understand, my code has no error, but it does. Can you give me a code for it?





    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class Resetter : MonoBehaviour
    7. {
    8.     public Rigidbody2D projectile;
    9.     public float resetSpeed = 0.025f;
    10.  
    11.     private SpringJoint2D spring;
    12.     private float resetSpeedSqr;
    13.     private readonly IEnumerable<GameObject> respawns;
    14.  
    15.     private void Start()
    16.     {
    17.             resetSpeedSqr = resetSpeed * resetSpeed;          
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (projectile)
    23.         {
    24.             if (!spring)
    25.                 spring = projectile.GetComponent<SpringJoint2D>();
    26.  
    27.             CheckForReset();
    28.         }
    29.         else
    30.         {
    31.             TryFindProjectile();
    32.         }
    33.     }
    34.  
    35.    
    36.  
    37.     void TryFindProjectile()
    38.     {
    39.         if (GameObject.FindGameObjectWithTag("Projectile"))
    40.         {
    41.             projectile = GameObject.FindGameObjectsWithTag("Projectile").GetComponent<Rigidbody2D>();
    42.             spring = projectile.GetComponent<SpringJoint2D>();
    43.          }
    44.  
    45.     }
    46.  
    47.  
    48.     void CheckForReset()
    49.     {
    50.         if(projectile.velocity.sqrMagnitude<resetSpeedSqr&&
    51.             spring == null)
    52.         {
    53.             Reset();
    54.         }
    55.  
    56.         if (Input.GetKeyDown(KeyCode.R))
    57.         {
    58.             Reset();
    59.         }
    60.  
    61.     }
    62.    
    63.     void OnTriggerExit2D(Collider2D other)
    64.     {
    65.         if (projectile && other.GetComponent<Rigidbody2D>() == projectile)
    66.         {
    67.            Reset();
    68.         }
    69.  
    70.  
    71.     }
    72.  
    73.     void Reset()
    74.     {
    75.         Destroy(projectile.gameObject);
    76.     }
    77.  
    78.  
    79. }
    80.  
     
  8. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    At quick glance, line 41 - you probably accidentally used:
    projectile = GameObject.FindGameObjectsWithTag


    Notice anything?

    You are using method that gets ...objects. You are not using findWithTag, which gets one object.

    FindGameObjectsWithTag returns an array, not a single object, so it can’t go into your variable, neither does it have a GetComponent.
     
    Last edited: Jan 14, 2021
    JFLGD, moikkaeel and PabloVortex like this.
  9. HKS3316

    HKS3316

    Joined:
    Apr 10, 2020
    Posts:
    4
    Code (CSharp):
    1. GameObject.FindGameObjectsWithTag
    //change this (Should Work)
    Code (CSharp):
    1. GameObject.FindGameObjectWithTag
     
  10. Athir-

    Athir-

    Joined:
    Jul 8, 2018
    Posts:
    4
    could you explain please, I am having same problem
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Please stop necro-posting old threads for unrelated problems.

    Make your own fresh post, it is free.

    Keep in mind many of the people above will never again log in and may never see your message.

    This means YOU MUST COMMUNICATE clearly what you are doing. WE CANNOT READ YOUR MIND.

    So... please start a fresh thread of your own, and before you post, read this:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    profitdefiant likes this.
Thread Status:
Not open for further replies.