3

I am working on a 3d basketball game in unity.

It has a score and a timer. After a certain time my scene loads and starts from the beginning. Every time I throw the ball, I have to spawn it.

It has a spawn button and a shoot button. But I don't want to use the spawn button. So I want to spawn the ball automatically.

How should I do it? I am giving my spawn button code and throw button code bellow.

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

public class SpawnButton: MonoBehaviour
{

    public GameObject ball;
    public GameObject BallPosition;

    public void Spawn()
    {
        ball.transform.position = BallPosition.transform.position;
        var ballPosition = ball.transform.position;
        ball.GetComponent<Rigidbody>().useGravity = false;
        ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
        ball.transform.position = ballPosition;
    }

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

public class ThrowButton: MonoBehaviour
{
    static Animator anim;
    public GameObject ball;
    public float ballThrowingForce = 5f;
    internal bool holdingBall;


    void Start()
    {
        anim = GetComponent<Animator>();
        ball.GetComponent<Rigidbody>().useGravity = false;
    }


    public void Throw()
    {
        anim.SetTrigger("isThrowing");
        StartCoroutine(Test());        
    }

    IEnumerator Test()
    {
        yield return new WaitForSeconds(1.5f);
        ball.GetComponent<Rigidbody>().useGravity = true;
        //ball.GetComponent<Rigidbody>().AddForce(transform.up * ballThrowingForce);
        ball.GetComponent<Rigidbody>().AddForce(0, 380.0f, ballThrowingForce);       
    }

}
Oen44
  • 3,156
  • 1
  • 22
  • 31
Sakib Rahman
  • 31
  • 1
  • 4
  • Not familiar with Unity, but are you using an event to determine when the ball has finished being thrown? If so, could you just add Spawn() to the end of that? – Hayden Dec 15 '19 at 11:01
  • 1
    Does this answer your question? [Unity - How to respawn a Gameobject after destroy](https://stackoverflow.com/questions/21504466/unity-how-to-respawn-a-gameobject-after-destroy) – Pavel Anikhouski Dec 15 '19 at 11:02
  • Can you be more specific, please? Didn't get it. – Sakib Rahman Dec 15 '19 at 11:04
  • @PavelAnikhouski well, my gameObject "ball" isn't destroying.. – Sakib Rahman Dec 15 '19 at 11:10
  • What is it that you wan to trigger the throw if not the button? but if you just want to keep it doing it you could use `InvokeRepeating` or change your current Test Coroutine to have a `while` loop with a delay for it. – akaBase Dec 15 '19 at 12:43

2 Answers2

1

To spawn a ball you should create a prefab and instantiate it. Example:

private class Spawner : MonoBehaviour
{
    public GameObject prefab;

    public GameObject Spawn() => Instantiate(prefab);
}

So that your throw code should spawn a ball and if you want destroy an older one.

misticos
  • 718
  • 5
  • 22
  • Why are you using a lambda here? Is it so you can just do `Spawn()` in other places, or do you have to use a lambda? – ThePikachuIH Jan 11 '21 at 17:42
  • There is no requirement or reason to, just saves space and looks nice unless you do something else. – misticos Jan 13 '21 at 10:59
1

The above answer by Iv Misticos mentioned the use of Instantiate which is a great way to spawn a new ball. before you spawn a new ball, you need to specify when it must spawn.

Either you can

  1. use a Invoke("SpawnNewBallMethod",3) //after this piece of code is executed, it waits for 3 seconds and executes the method you mention in it which can have the code to instantiate.

  2. Or After the first ball's work is done, you can destroy it(Destroy(ball)) or set active to false - Then check the state (if it exists(null check?) or gameObject.active==true) and accordingly instantiate a new ball (if it is setactive(false) don't forget to destroy it later).

Although gameObject.active is obsolete, it will serve the purpose without complicating things. In my opinion, Invoke is better than a IEnumerator here as Invoke will not stop the execution flow and continue with the other things while the timer is running.

Chandradhar
  • 37
  • 1
  • 9