Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

GameObject does not contain a definition for defined GameObject.

Discussion in 'Scripting' started by Sergal, Aug 29, 2017.

  1. Sergal

    Sergal

    Joined:
    Nov 29, 2016
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Movecheck : MonoBehaviour
    {
    void Start()
    {
    GameObject playerBlue = GameObject.FindWithTag("Players");
    GameObject rb2d = GameObject.playerBlue.Rigidbody2D;
    }

    public int playerhasmoved = 0;

    void Update()
    {

    if (rb2d.velocity.magnitude > 0)
    {
    playerhasmoved = 1;
    }
    }

    I have tried so many variations of this, so many times over the course of a week and I have no idea what's causing it. Error on line 10 states that GameObject playerBlue is not defined, thus line 18 (The if statement) claims rb2d does not exist in current context. Other variations include:

    GameObject.FindWithTag("Players") playerBlue;
    GameObject.playerBlue.Rigidbody2D rb2d;
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,503
  3. Sergal

    Sergal

    Joined:
    Nov 29, 2016
    Posts:
    2
    I've spent the last 3 weeks going through those tutorials, far before I started this. I need an explanation as to why it's not recognizing the defined GameObject. Why is it so hard to explain why it's not recognizing the Rigidbody2D? Or offering a workaround snippet that would solve something I've been stuck on for a week now, as I've said?
     
    AmazinJacks likes this.
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,503
    Because your syntax is wrong and, even if it were correct, your variables are only scoped to your Start method; that's why I suggested the tutorials. :)

    To your specific issue:
    Code (csharp):
    1.  
    2. private GameObject playerBlue;
    3. private RigidBody2D rb2d;
    4.  
    5. void Start()
    6. {
    7.     playerBlue = GameObject.Find(....);
    8.     rb2d = playerBlue.GetComponent<RigidBody2D>();
    9. }
    10.  
     
    Sergal likes this.
  5. AmazinJacks

    AmazinJacks

    Joined:
    Oct 22, 2019
    Posts:
    8
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,503
    Probably more helpful than replying uselessly 3 years after the fact, but sure I'll bite. I offered that link because it was apparent that OP did not have a grasp of basic programming concepts.

    Also, 3 years ago, when I posted that link, Learn did not look like it does now and that page did not have 241 tutorials on it and instead offered a good primer on getting started with programming. For the record.
     
  7. ljohnson23

    ljohnson23

    Joined:
    Jun 16, 2020
    Posts:
    1
  8. FireBlazingDev

    FireBlazingDev

    Joined:
    Sep 28, 2020
    Posts:
    4
    Assets\Scripts\Player Scripts\PlayerMovement.cs(28,16): error CS1061: 'Rigidbody2D' does not contain a definition for 'Addforce' and no accessible extension method 'Addforce' accepting a first argument of type 'Rigidbody2D' could be found (are you missing a using directive or an assembly reference?)

    thats my error for my code, im making a 2d platform game and trying to make walking and jumping movements, i really dont know what to do, it says that Rigidbody2D does not contain a definition for Áddforce, here is my code for reference

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {

    public float speed;
    public float jump;

    private float move;
    private Rigidbody2D rb;
    private bool isJumping;
    // Start is called before the first frame update
    void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
    move = Input.GetAxis("Horizontal");

    rb.velocity = new Vector2(move * speed,rb.velocity.y);
    if (Input.GetButtonDown("Jump") && !isJumping)
    {
    rb.GetComponent<Rigidbody2D>(). Addforce(new Vector2(rb.velocity.x, jump));
    isJumping = true;
    }
    }

    void OnCollisionEnter2D(Collision2D other)
    {
    if (other.gameObject.CompareTag("Ground"))
    {
    isJumping = false;
    }
    }
    }

    Please reply as fast as u can,
    Thanks in advance
     
  9. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    103
    Because it´s written with a big letter F: AddForce()
     
    Joe-Censored likes this.
unityunity