Search Unity

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

Discussion in 'Getting Started' started by glitch100xYT, Feb 19, 2024.

  1. glitch100xYT

    glitch100xYT

    Joined:
    Jan 30, 2024
    Posts:
    2
    im trying add movement to my game by following the guide from this video:
    (24:45 is where im at) and it gave me the game object error and this is my code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class player_movement : MonoBehaviour
    {
    private Rigidbody2D rb2D;
    private float moveSpeed;
    private float moveHorizontal;

    void start()
    {
    rb2D = gameObject.GetComponet<Rigidbody2D>();
    moveSpeed = 3f;

    }

    // Update is called once per frame
    void Update()
    {
    moveHorizontal = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
    if(moveHorizontal > 0.1f || moveHorizontal < -0.1f)
    {
    rb2D.AddForce(new Vector2(moveHorizontal * moveSpeed, 0f), ForceMode2D.Impulse);
    }
    }


    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    If you're copying code you don't understand then you need to know that you have to type everything exactly.

    I don't see why you'd get the error you did so maybe it's just an error from somewhere else you made a mistake too but the code you posted here didn't spell "GetComponent" correctly:
    Code (CSharp):
    1. rb2D = gameObject.GetComponet<Rigidbody2D>();
    Here's how to post code on the forums btw: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. glitch100xYT

    glitch100xYT

    Joined:
    Jan 30, 2024
    Posts:
    2
    thanks man i now know i spelt getcomponent wrong that way and i didn't know how to post the code so thanks for that