- Home /
Question by
Haileapp · Aug 07, 2017 at 02:08 PM ·
2d gamephysics2dspringjoint
Problem on Spring Joint 2D
I am working on my 2D Angrybird type game.When the ball is destroyed, I instantiate the ball with All its Components using prefab.But when the springjoint2d is destroyed and the ball is instantiated, it says ' There is no 'SpringJpint2D' attached to the Asteroid(Clone) game object, but a script is trying to access it'. Here is the Code...
public class projectile : MonoBehaviour {
private float maxStretch = 8f;
public LineRenderer catapultLineFront;
public LineRenderer catapultLineBack;
public static projectile current;
private SpringJoint2D spring;
private Ray rayToMouse;
private float maxStretchSqr;
private Ray leftCatapultToProjectile;
private float circleRadius;
private Transform catapult;
private bool clickedOn;
private Vector2 prevVelocity;
private Rigidbody2D rig2D;
private Score scoring;
private int valueDeducted = 10;
private GameObject Atta;
private AudioSource playerBounce;
private float minVelocity = 30.0f;
public void Awake ()
{
spring = GetComponent<SpringJoint2D> ();
catapult = spring.connectedBody.transform;
rig2D = GetComponent<Rigidbody2D> ();
}
public void Start () {
LineRendererSetup ();
GameObject myScore = GameObject.FindGameObjectWithTag ("Score");
playerBounce = gameObject.GetComponent <AudioSource>();
scoring = myScore.GetComponent<Score> ();
rayToMouse = new Ray(catapult.position,Vector3.zero);
leftCatapultToProjectile = new Ray(catapultLineFront.transform.position,Vector3.zero);
maxStretchSqr = maxStretch * maxStretch;
CircleCollider2D circle = GetComponent() as CircleCollider2D;
circleRadius = circle.radius;
}
public void FixedUpdate ()
{
int XPos = Random.Range (-53,-54);
int YPos = Random.Range(-15,-16);
if (clickedOn)
Dragging ();
if (spring != null) {
if (!rig2D.isKinematic && prevVelocity.sqrMagnitude > rig2D.velocity.sqrMagnitude) {
Destroy (spring);
// If you see public bool with a get set {} tag,it means you should use it with the equal sign parameter and not with ()
rig2D.velocity = prevVelocity;
LineRendererUpdate ();
}
if (!clickedOn)
prevVelocity = rig2D.velocity;
LineRendererUpdate ();
}
if (spring == null && rig2D.velocity.magnitude < minVelocity) {
StartCoroutine (destroyPlayer());
GameObject ballClone = Instantiate (gameObject, new Vector2(XPos,YPos), transform.rotation) as GameObject;
ballClone.AddComponent <Rigidbody2D>();
DontDestroyOnLoad (spring);
if (FindObjectsOfType (GetType ()).Length > 1) {
Destroy (gameObject);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
how to control amount of force taken from the surface 0 Answers
Is there a standardized way for creating a 2D ball physics movement method? 0 Answers
GameObject not detecting collision with floor 0 Answers
Change a specific Gameobjects rigidbody gravity to horizontal (left) 1 Answer
Default Contact Offset Problems 1 Answer