S.O.S - Pong in trouble - Need help!

With a smile on my mouth i write this as i’m about to cry because i can’t laugh anymore.

My darn pong game won’t work.

Or rather.

Everything works but whenever i loose a point it crashes to white screen
The ball bounces well enough of off everything. but as soon as it hit my

Enemy or Player dead zone it crashes to a white screen.

These 2 are my trigger’s which are just a empty game object with a box collider and is a trigger ticked
here it is (this is the enemy dead zone one)

After i added my score script to both it stopped working… It crashes when i score a goal or the enemy does.

This is the script of the Score.cs

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour {
   
    public TextMesh currSco;
    public GameObject ballPref;
    public Transform paddleObj;

    GameObject Ball;
    int score;
    
    void Update ()
    {
        Ball = GameObject.FindGameObjectWithTag("Ball");
        currSco.text = "" + score;
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Ball")
        {
            score += 1;
            Destroy(Ball);
            (Instantiate(ballPref, new Vector3(paddleObj.transform.position.x + 2, paddleObj.transform.position.y,0), Quaternion.identity) as GameObject).transform.parent = paddleObj;
        }
    }
}

Please help here…

My ball is tagged Ball and the name is ball

Sincerely - Tereith -

  • Ball = GameObject.FindGameObjectWithTag(“Ball”);

Place that in the OnTrigger method.
Also, split up the Instantiate code

var ball = Instantiate(ballPref, new Vector3(paddleObj.transform.position.x + 2, paddleObj.transform.position.y,0), Quaternion.identity) as GameObject;
ball.transform.parent = paddleObj;

That’ll at least makes it easier to use the ‘Debug.log()’ code to debug it. Debug both ‘Ball’ and ‘ball’ after instantiating them and see if either are null.

ok it now looks like so

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour {
   
    public TextMesh currSco;
    public GameObject ballPref;
    public Transform paddleObj;

    GameObject Ball;
    int score;
    
    void Update ()
    {
        Ball = GameObject.FindGameObjectWithTag("Ball");
        currSco.text = "" + score;
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Ball")
        {
            score += 1;
            Destroy(Ball);
            var ball = Instantiate(ballPref, new Vector3(paddleObj.transform.position.x + 2, paddleObj.transform.position.y,0), Quaternion.identity) as GameObject;
            ball.transform.parent = paddleObj;
        }
    }
}

What do you mean by putting

Never mind that, just replace Destroy(Ball); with Destroy(other.GameObject) as the other gameobject IS the Ball.

Do you get any errors in the Unity console btw?

I have no error in the console at the moment. Let me try and change those 2 and play the game. :slight_smile:

got those 2

without seeing the revised version… … you’re missing a ; somewhere in your code… like it says, probably somewhere around line 25

Looks like it.

lowercase g
add a ; at the end of that line

GameObject => class
gameObject => the gameobject this script is attached to
something.gameObject => the gameobject belonging to something

Thanks now it’s like this :slight_smile:

I’m sorry very new to scripting.

Ok got these 2 warnings.

No idea what it might cause. I still get the white screen crash whenever i score a point…

ok, those two warning are “you’ve created a variable you aren’t using”, not going to cause any issues just a waste of memory assignments.

Can you post your current code after the tweaks.

Well your second error message is just telling you that your Ball variable is not being used anymore after you destroy your ball. You could fix that by changing:

var ball = Instantiate(ballPref, new Vector3(paddleObj.transform.position.x + 2, paddleObj.transform.position.y,0), Quaternion.identity) as GameObject;

to just reuse the same variable:

Ball = Instantiate(ballPref, new Vector3(paddleObj.transform.position.x + 2, paddleObj.transform.position.y,0), Quaternion.identity);

Thanks idk what happened but somehow i managed to fix it. It’s now working properly with music and everything. Thanks a lot for your help guys! :slight_smile:
been to a big help