Code provided doesn't seem to work (Ball Respawn)

Hey!

If any of you guys have a problem with this lesson here is the code that worked for me.

I tried pasting the code in the git commit but it didn’t work.

In the video, we are asked to Get the SpringJoint component of the ball but there isn’t one. So I made a Serialized field where i put the Spring Joint. I tried a lot of solutions and checked again and again if the code of the course worked but it didn’t. Anyway here is my code.

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

public class BallHandler : MonoBehaviour
{
    [SerializeField] private GameObject ballPrefab;
    [SerializeField] private Rigidbody2D pivot;
    [SerializeField] private SpringJoint2D spring_joint; //ADDED
    [SerializeField] private float detachDelay;
    [SerializeField] private float respawnDelay;

    private Rigidbody2D currentBallRigidbody;
    private SpringJoint2D currentBallSprintJoint;

    private Camera mainCamera;
    private bool isDragging;

    // Start is called before the first frame update
    void Start()
    {
        mainCamera = Camera.main;

        SpawnNewBall();
    }

    // Update is called once per frame
    void Update()
    {
        if (currentBallRigidbody == null) { return; }

        if (!Touchscreen.current.primaryTouch.press.isPressed)
        {
            if (isDragging)
            {
                LaunchBall();
            }

            isDragging = false;

            return;
        }

        isDragging = true;
        currentBallRigidbody.isKinematic = true;

        Vector2 touchPosition = Touchscreen.current.primaryTouch.position.ReadValue();

        Vector3 worldPosition = mainCamera.ScreenToWorldPoint(touchPosition);

        currentBallRigidbody.position = worldPosition;
    }

    private void SpawnNewBall()
    {

        GameObject ballInstance = Instantiate(ballPrefab, pivot.position, Quaternion.identity);

        currentBallRigidbody = ballInstance.GetComponent<Rigidbody2D>();
        currentBallSprintJoint = spring_joint; //CHANGED
        currentBallSprintJoint.enabled = true;  //ADDED

        currentBallSprintJoint.connectedBody = currentBallRigidbody;
    }

    private void LaunchBall()
    {
        currentBallRigidbody.isKinematic = false;
        currentBallRigidbody = null;

        Invoke(nameof(DetachBall), detachDelay);
    }

    private void DetachBall()
    {
        currentBallSprintJoint.enabled = false;
        currentBallSprintJoint = null;

        Invoke(nameof(SpawnNewBall), respawnDelay);
    }
}


There should have been a SpringJoint2D component on the ball…

1 Like

Thank you so much for pointing this out.

Just wanted to provide feedback on this mistake as such small things could be quite detrimental for a beginner. I bought this course a while ago, when it came out and started working on it, however, when i got to this lecture back then i got stuck for a few days trying to figure it out and nothing worked…should have approached the community of course, but i didn’t…i simply stopped learning then. Now after over a year i am getting back to this course and got stuck on this lecture again, luckily this time i went to check the forum and found your solution. I am now quite angry on my self that i did not do it then and lost a lot of time.

Please correct it in the main lecture because this is VERY confusing.

Privacy & Terms