Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Unity coding error

Started by JuliaAxt May 16, 2018 at 4:41 PM 7 replies 1.8k views
Original Post
JuliaAxt
JuliaAxt

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();
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 tapForcedoes 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
}

}


}

Lactose
Lactose
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.

Hello to all my stalkers.
JuliaAxt
JuliaAxt

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

Nypyren
Nypyren

The first error is due to a typo.

JuliaAxt
JuliaAxt
50 minutes ago, Nypyren said:

The first error is due to a typo.

Thank you!

frob
frob

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 tapForcedoes 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?

JuliaAxt
JuliaAxt
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

Nypyren
Nypyren
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.