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

Question Help I am getting a error for "GameObject[] does not contain a definition for GetComponent

Discussion in 'Scripting' started by ThatDudeWasTaken, Jun 27, 2023.

  1. ThatDudeWasTaken

    ThatDudeWasTaken

    Joined:
    Jun 8, 2023
    Posts:
    1
    This is my code

    using UnityEngine;
    public class PipeMiddleScript : MonoBehaviour
    {
    public LogicScript logic;
    // Start is called before the first frame update
    void Start()
    {
    logic = GameObject.FindGameObjectsWithTag("Logic").GetComponent<LogicScript>();
    }
    // Update is called once per frame
    void Update()
    {

    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
    logic.addScore();
    }
    }
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,833
    First, use code tags to make code readable on the forum.

    Second,
    GameObject.FindGameObjectsWithTag()
    returns an array of matching objects. When the error message says "GameObject[] does not contain a definition..." it means there is no such method for an array of GameObjects. Instead, the GameObject class itself has that method. You need to pick an instance and call it, or you need to iterate through your array of objects and call it on each one.

    You may even want to capture the list of such GameObject instances that are just a child of this one, for later use, such as
    GameObject.GetComponentsInChildren<T>()
    .

    From what little you've given, I can't know which would be the right thing to do.
     
  3. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    280
    The GameObject.FindGameObjectsWithTag(string) method returns a GameObject array. The array has no GetComponent method. You need to index into the array of multiple GameObjects to call GetComponent on a specific element in the array.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    ^ ^ ^ This is super-uber crazy advanced ninja code!

    Keep it simple: make public fields and drag the objects in, or else make a simple central singleton locator pattern to find your logic. Seriously.

    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066

    In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

    If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way(tm) success of accessing things in your game.


    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

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

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums
     
    Last edited: Jun 27, 2023
    Ryiah and CodeRonnie like this.
  5. desoky231

    desoky231

    Joined:
    Jul 14, 2023
    Posts:
    1
    Code (CSharp):
    1. GameObject.FindGameObjectsWithTag("Logic").GetComponent<LogicScript>();
    this line if you used auto-completion return an array of matching object but when you write it without an S

    like that
    Code (CSharp):
    1. logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<logicScript>();
    it would make it so that you only have on object with the tag Logic and it would be assigned to logic

    I was also watching the https://www.youtube.com/watch?v=XtQMytORBmM for flappy bird and it happened to me.
     
    Pooja_Amin likes this.