Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# GameObject[] error

Hi, I'm a beginner and recently this error popped up on me, do you know how to fix it?
PS: I wanted to program a motion animation (according to the tutorial).

Error name:

GameObject[] does not contain a definition for GetComponent and no available GetComponent extension method was found that accepts the first argument of the GameObject[] type.

Code:

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Move : MonoBehaviour
{
    Rigidbody rb;
    Animator objectwithAnim;

    // Start is called before the first frame update
    void Start()
    {
        rb = transform.GetComponent<Rigidbody>();
        objectwithAnim = GameObject.FindGameObjectsWithTag("Animobject").GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.D))
        {
            rb.AddRelativeForce(new Vector3(20, 0, 0));
            objectwithAnim.SetBool("Walk", true);
        }
        if (Input.GetKey(KeyCode.A))
        {
            rb.AddRelativeForce(new Vector3(-20, 0, 0));
            objectwithAnim.SetBool("Walk", true);
        }
        if (Input.GetKey(KeyCode.W))
        {
            rb.AddRelativeForce(new Vector3(0, 0, 20));
            objectwithAnim.SetBool("Walk", true);
        }
        if (Input.GetKey(KeyCode.S))
        {
            rb.AddRelativeForce(new Vector3(0, 0, -20));
            objectwithAnim.SetBool("Walk", true);
        }
        if (Input.GetKeyUp(KeyCode.W)  || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.D))
        {
            objectwithAnim.SetBool("Walk", false);
        }
    }
}
 
Hello, @LukasProgrammer.

I seen your other thread so I'm responding to this one. Please don't bother reposting questions because staff will end up taking them down.

I haven't worked with Unity in a long time so I apologise if I don't get anything right. To start with, the code looks correct, so with this, I suggest checking the object this script is attached to. Because you have declared rigidbody and animator fields, Unity will show these fields of the object in the editor(unless they are declared private) and you can then attach the necessary assets - have you done that?
 
Hello, BorkedSystem32 and thank you for your feedback,. I checked the object and I think there is no problem. I was puzzled by your last sentence though. I know this question will probably sound stupid now, but, what do you mean by Editor, I don't know where to look for it, or what exactly is meant.

Also, thanks for your advice about repetitively posting questions.
 
@LukasProgrammer, when I said "editor" I perhaps should've specified Unity editor(where you develop and design your game in) - not the code editor you write your scripts in.

To further clarify what I meant, use this image from the Unity manual:

InspectorWindowCallout.jpginspectorfields.jpg

The 2nd image is what I meant by "fields of the object" - I believe this is where your error lies, because the code in both your start and update functions look correct. I also think the problem may be to do with not declaring your variables as "public" and because of that, they won't show in the inspector(or so I think. Again, I haven't worked with Unity in a long time).
 
I know you stated you solved the issue, but I believe the issue stems from GameObject.FindGameObjectsWithTag("Animobject") returning an array of GameObjects.
Therefore you can't access the GetComponent function directly from the results which in this case is an array of GameObjects.
You can however access an element in the objectswithAnim array.

C#:
Animator firstAnimator = objectsWithAnim[0].GetComponent<Animator>();
Code:
 
GameObject.FindGameObjectsWithTag returns an array of Game Objects

if you are simply trying to find '1' game object with a tag you should use

GameObject.FindWithTag("Animobject").GetComponent<Animator> which returns just a single object
more specifically it will return the result as doing this

GameObject.FindGameObjectsWithTag("Animobject")[0].GetComponent<Animator>
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom