Jump to content
  • Advertisement

Recommended Posts

Posted (edited)

Please help me with this code, this error is currently stopping my project 

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

[RequireComponent(typeof(Rigidbody2D))]
public class TapController : MonoBehaviour {

    public float tapForence = 10;
    public float tiltSmooth = 5;
    public Vector3 startPos;

    Rigidbody2D Rigidbody;
    Quaternion downRotation;
    Quaternion forwardRotation;

    private void Start() {
        Rigidbody = GetComponent<Rigidbody2D>();
        downRotation = Quaternion.Euler(0, 0, -90);
        forwardRotation = Quaternion.Euler(0, 0, 35);
  
    }

    private void Update() {
        if (Input.GetMouseButtonDown(0))
        {
            transform.rotation = forwardRotation;
            Rigidbody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);   (The name `tapForce`does not exist in current context)
        }

        transform.rotation = Quaternion.Lerp(transform.rotation, downRotation, tiltSmooth * Time.deltaTime);
    }
}

    void OnTriggerEnter2D(Collider2D col){
    if (col.gameObject.tag == "scoreZone")
    {
        // register a score event
        // play a sound
    }
    if (col.gameObject.tag == "deadZone")
    {
        Rigidbody.simulated = false;   (`Rigidbody` does not contain a definition for `simulated´) 
        //register a dead event
        //play a sound
    }

     }


}
 

Edited by JuliaAxt

Share this post


Link to post
Share on other sites
Advertisement
1 minute ago, JuliaAxt said:

Please help me with this code, or else my teacher will fail me ;(

This forum generally doesn't help out with homework or school assignments. Talk to your teacher and/or other students in your class.

Share this post


Link to post
Share on other sites

I totally respect this rule. The problem is that my teacher is a beginner just like me. I would appreciate all possible help

Share this post


Link to post
Share on other sites
Posted (edited)
50 minutes ago, Nypyren said:

The first error is due to a typo.

Thank you!

Edited by JuliaAxt

Share this post


Link to post
Share on other sites

The homework rule is along the lines of "we won't do your homework", asking about specific issues in code is generally fine.  (It gets hard to tell when the homework issue is also the code issue, but that's rarely the case.)

 

39 minutes ago, JuliaAxt said:

public float tapForence = 10;

Rigidbody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);   (The name `tapForce`does not exist in current context)

As Nypyren pointed out, you've got a typo. Those words are not the same, but you probably intended them to be.

 

40 minutes ago, JuliaAxt said:

Rigidbody.simulated = false;   (`Rigidbody` does not contain a definition for `simulated´) 

I'm not sure what you are trying to do there. Rigidbody is a component class rather than an object, it doesn't have that as a property. Here are the docs.

What did you hope to accomplish with that line?

Share this post


Link to post
Share on other sites
17 minutes ago, frob said:

I'm not sure what you are trying to do there. Rigidbody is a component class rather than an object, it doesn't have that as a property. Here are the docs.

What did you hope to accomplish with that line?

It is at the point in the game when the character reaches a 'deadzone' and the player dies. I hope im clear enough

Share this post


Link to post
Share on other sites
Posted (edited)
7 hours ago, frob said:

I'm not sure what you are trying to do there. Rigidbody is a component class rather than an object, it doesn't have that as a property. Here are the docs.

In C# you can have variables with the same name as types, and the meaning is taken from the context.  In the above code there is a member field with the name 'Rigidbody'.  This should work.  However, the code above looks like there are too many closing } so the OnTriggerEnter2D doesn't seem to be in the same class.

The above code shouldn't compile due to that extra }, but this might just be a copy-paste issue when posting the code here.  If the OnTriggerEnter2D is in a different class where the 'Rigidbody' variable is no longer in scope, it will fall back to attempting to access a static member of the 'Rigidbody' type instead, which would explain the error in the original post.

Edited by Nypyren

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!