how to make trampoline in unity 3D

I am just making maze game that rotates stage to move the ball to move into a goal and I am planning to make trampoline that bounce a ball, however although I use other webpage’s explanations, it seems not working?
What’s goes wrong and how can I fix this code to make ball bounce when ball collide the plate which contains this code below?

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

public class Bouncing : MonoBehaviour
{
    public float BouncingForce = 2f;
    public Rigidbody PlayerBall;

    private void Start()
    {
        PlayerBall = gameObject.GetComponent<Rigidbody>();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
            PlayerBall.AddForce(Vector3.up * BouncingForce);
    }
}

It seems that you are not actually referencing the ball object within your game.

On line 12 where you assign a rigidbody to the PlayerBall object it is not the ball’s rigidbody being assigned. It is the rigidbody of the game object the script is attached to.

In order to reference the ball object you will have to use some other way to reference it.


Using GameObject.Find()

One way would be the Find() method.

To use it, you would do something like this:

GameObject.Find("NameOfYourBallObject").GetComponent<Rigidbody>();

This method uses “GameObject”. The capitalization of the G changes its meaning compared to “gameObject”. “GameObject” is just a general call to something of type GameObject within the scene.

The Find() method takes the name of the game object you are looking for as a string, so type the name of your ball object in there.


Using GameObject.FindWithTag()

This way would be similar to the Find() method.

GameObject.FindWithTag("TagOfBallObject").GetComponent<Rigidbody>();

Instead of inputting the name of the object as the parameters of the function, you would be using the tag assigned to it.


So long as you do either of these options in place of your 12th line it should work, just remember to change to parameters to match your object.