- Home /
Question by
dBlue · Dec 26, 2015 at 08:15 PM ·
c#rigidbodyfreezepositionsyntax error
I dont understand the syntax of RigidbodyConstraints2D and freezeposition, I keep getting an error C#
My problem is only in the last collision function. Neither that line nor the line commented out work. The error is that I need to qualify it with a name, but isn't that what I'm doing when I say "GameObject.Find("Player01")? Thanks in advance, this shouldn't be a big problem but I've been totally stuck on it.
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
public float speed = 1f;
public float jumpSpeed;
public Rigidbody2D rb;
void Start () {
rb = GetComponent<Rigidbody2D>();
}
void Update () {
if (Input.GetKey (KeyCode.D) && (transform.position.x < 3.5)) {
transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0f);
transform.localScale = new Vector3 (2.25f, 2.25f, 0f);
}
if (Input.GetKey (KeyCode.A) && (transform.position.x > -3.5)) {
transform.position -= new Vector3 (speed * Time.deltaTime, 0.0f, 0f);
transform.localScale = new Vector3 (-2.25f, 2.25f, 0f);
}
if (Input.GetKeyDown (KeyCode.S)) {
gameObject.GetComponent<Rigidbody2D> ().gravityScale += 1;
}
if (Input.GetKeyDown (KeyCode.W)) {
gameObject.GetComponent<Rigidbody2D> ().gravityScale -= 1;
}
if (Input.GetKeyDown (KeyCode.Space)) {
rb.AddForce(transform.up * jumpSpeed);
}
}
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.name == "Platform01") {
gameObject.GetComponent<RigidbodyConstraints2D>().FreezePosition = true;
//GameObject.Find("Player01").GetComponent<RigidbodyConstraints2D>().FreezePosition = true;
}
}
}
Comment
Best Answer
Answer by pako · Dec 26, 2015 at 09:03 PM
RigidbodyConstraints2D is not a component, it's an enumeration.
You have to use:
gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePosition;
Your answer
Follow this Question
Related Questions
Freeze rigidbody position in script 5 Answers
Multiple Cars not working 1 Answer
cannot have all constraints on in rigidbody? 1 Answer
Distribute terrain in zones 3 Answers
RigidbodyConstraints.FreezePositionY doesn't freeze the position 1 Answer