Search Unity

GameObject does not contain a definition for defined GameObject.

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

Thread Status:
Not open for further replies.
  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,539
    Bunny83 likes this.
  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,539
    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:
    9
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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:
    267
    Because it´s written with a big letter F: AddForce()
     
    Joe-Censored likes this.
  10. NikolasD2006

    NikolasD2006

    Joined:
    Nov 10, 2021
    Posts:
    1
    Can someone help me aswell.
    Im trying to delete an object when it collides with another specified object.
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class DestroyBall : MonoBehaviour
    {
    void OnTriggerEnter2D(Collider2D other)
    {
    if (other.gameObject.Player == "Character")
    {

    Destroy(other.gameObject);
    }
    }
    }

    Im getting the same definition error.
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Please STOP responding to four-year-old random threads for random typographic errors.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
Thread Status:
Not open for further replies.