LukasProgrammer
New Coder
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:
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);
}
}
}