I need help with Deleting and Initiating a gameobject
Hi, I am working a Pong Remake game and I am lost on how to delete the gameobject and initiate it again from it's default spawn position
I have a scoring system so in the code below right at the bottom you will see the points system
FYI It is a Ball for the game CODE:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.UI;
public class Ball : MonoBehaviour
{
public float speed = 12;
public GameObject original;
public Text countTextL;
public Text countTextR;
private int countL;
private int countR;
void Start()
{
countL = 0;
countR = 0;
SetCountTextL();
SetCountTextR();
// Initial Velocity
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
}
float hitFactor(Vector2 ballPos, Vector2 racketPos,
float racketHeight)
{
return (ballPos.y - racketPos.y) / racketHeight;
}
void OnCollisionEnter2D(Collision2D col)
{
// Note: 'col' holds the collision information. If the
// Ball collided with a racket, then:
// col.gameObject is the racket
// col.transform.position is the racket's position
// col.collider is the racket's collider
if (col.gameObject.name == "RacketLeft")
{
// Calculate hit Factor
float y = hitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);
// Calculate direction, make length=1 via .normalized
Vector2 dir = new Vector2(1, y).normalized;
// Set Velocity with dir * speed
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
if (col.gameObject.name == "RacketRight")
{
// Calculate hit Factor
float y = hitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);
// Calculate direction, make length=1 via .normalized
Vector2 dir = new Vector2(-1, y).normalized;
// Set Velocity with dir * speed
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
if (col.gameObject.name == "WallLeft")
{
countR = countR + 1;
SetCountTextR();
if (countR >= 3)
{
SceneManager.LoadScene("RightSide Scene");
}
}
if (col.gameObject.name == "WallRight")
{
countL = countL + 1;
SetCountTextL();
if (countL >= 3)
{
SceneManager.LoadScene("LeftSide Scene");
}
}
}
void SetCountTextL()
{
countTextL.text = countL.ToString();
}
void SetCountTextR()
{
countTextR.text = countR.ToString();
}
}
Answer by toddisarockstar · Feb 19, 2017 at 08:26 PM
//spawn something;
Instantiate (gameObject, Vector3.zero, Quaternion.identity);
//object will self destruct!!!
Destroy (gameObject);
in your situation i dont think the proficiency would matter in your game but as a general rule in game development its not considered good habit to actuall destroy things when you want them to temporarily dissapear. I would rececoment simply snapping the ball's position off camera or temporarily making it transparent instead till you want to move it back into game play.
I see what you mean. When I tried it, the ball hit the wall paused then continued moving then it wouldn't do anything
I can't get the ball to reset to the middle That is all I need to happen is the ball to go back to the middle where it first spawns.
Vector3 startspot;
void start(){
startspot=transform.position;
}
void update(){
when you wan the ball to come back just say
transform.position = startspot;
}