I'm having an issue with my script and I don't have a clue what is wrong.
The error I get is:
Assets/Scripts/BlackBirdDrag.cs(43,41): An object reference is required to access non-static member `UnityEngine.Rigidbody2D.isKinematic'
Here is the script:
using UnityEngine;
using System.Collections;
public class BlackBirdDrag : MonoBehaviour {
public float maxStreatch = 3.0f;
public LineRenderer catapultLineFront;
public LineRenderer catapultLineBack;
private SpringJoint2D spring;
private Transform catapult;
private Ray rayToMouse;
private Ray leftCatapultToProjectile;
private float maxStretchSqr;
private bool clickedOn;
private float circlRadius;
private Vector2 prevVelocity;
//private GameObject circle;
void Awake () {
spring = GetComponent ();
catapult = spring.connectedBody.transform;
}
void Start () {
LineRendererSetup ();
rayToMouse = new Ray(catapult.position, Vector3.zero);
leftCatapultToProjectile = new Ray(catapultLineFront.transform.position, Vector3.zero);
maxStretchSqr = maxStreatch * maxStreatch;
CircleCollider2D circle = Collider2D as CircleCollider2D;
circlRadius = circle.radius;
}
void Update () {
if(clickedOn)
Dragging ();
if(spring != null) {
if(!Rigidbody2D.isKinematic && prevVelocity.sqrMagnitude > Rigidbody2D.velocity.sqrMagnitude) {
Destroy (spring);
Rigidbody2D.velocity = prevVelocity;
}
if(!clickedOn)
prevVelocity = Rigidbody2D.velocity;
LineRendererupdate ();
}else{
catapultLineFront.enabled = false;
catapultLineBack.enabled = false;
}
}
void LineRendererSetup () {
catapultLineBack.SetPosition(0,catapultLineBack.transform.position);
catapultLineFront.SetPosition(0,catapultLineFront.transform.position);
catapultLineBack.sortingLayerName = "foreground";
catapultLineFront.sortingLayerName = "foreground";
catapultLineBack.sortingOrder = 1;
catapultLineFront.sortingOrder = 3;
}
void OnMouseDown () {
spring.enabled = false;
clickedOn = true;
}
void OnMouseUp () {
spring.enabled = true;
Rigidbody2D.isKinematic = false;
clickedOn = false;
}
void LineRendererupdate () {
Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position;
leftCatapultToProjectile.direction = catapultToProjectile;
Vector3 holdpoint = leftCatapultToProjectile.GetPoint(catapultToProjectile.magnitude + circlRadius);
catapultLineBack.SetPosition(1, holdpoint);
catapultLineFront.SetPosition(1, holdpoint);
}
void Dragging () {
Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
if(catapultToMouse.sqrMagnitude > maxStretchSqr) {
rayToMouse.direction = catapultToMouse;
mouseWorldPoint = rayToMouse.GetPoint(maxStreatch);
}
mouseWorldPoint.z = 0f;
transform.position = mouseWorldPoint;
}
}
Answer
I'm not familiar with Unity.
Your issue is that you try to call methods on a Class instead of calling methods on an instance of a Class, i.e. on an Object.
Specifically, the issue shows up in two places:
First, in Update()
if(spring != null) {
if(!Rigidbody2D.isKinematic ...
and later, in OnMouseUp()
void OnMouseUp () {
spring.enabled = true;
Rigidbody2D.isKinematic = false;
clickedOn = false;
}
RigidBody2D doesn't have a static member called isKinematic. Only instances of it have such a member.
You must fetch the instance of the Rigidbody2D component and call the methods on that instead.
Here is an example (retrieved from here).
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Rigidbody rb;
void Start() {
rb = GetComponent();
}
void FixedUpdate() {
rb.AddForce(Vector3.up);
}
}
Then you'll be able to call the functions on the variable rb
if you follow the example. ie. rb.isKinematic
No comments:
Post a Comment