- Home /
How to create Game Object 'n' times?
Hey Friends! I am working on a game and in which i want to through the ball using a spring joint. i want to through the ball n times but i don't want to create objects manualy. Is there any way to create objects using Script. i am using this code for my ball. Thanx
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Ball : MonoBehaviour {
public AudioClip Hit,leave;
private bool isPressed = false;
public Rigidbody2D rb;
public Rigidbody2D Holder;
public float releaseTime = .15f, loadtime;
public float maxDragDistance= 2.5f;
public GameObject nextBall;
public Text text;
public static int remaininghits=10;
public LevelManager levelmanager;
public TextControll textcontroll;
// Use this for initialization
void Start(){
levelmanager = GameObject.FindObjectOfType<LevelManager> ();
levelmanager = GameObject.FindObjectOfType<LevelManager> ();
}
void Update(){
if (isPressed) {
//capture mouse pos;
Vector2 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
if (Vector3.Distance (mousePos, Holder.position) > maxDragDistance)
rb.position = Holder.position + (mousePos - Holder.position).normalized * maxDragDistance;
else
rb.position = mousePos;
}
}
void OnCollisionEnter2D(Collision2D Collision){
AudioSource.PlayClipAtPoint (Hit, transform.position);
Debug.Log ("Collision Detected");
}
void OnMouseDown () {
isPressed = true;
rb.isKinematic = true;
}
void OnMouseUp () {
//on leaving the mouse
isPressed = false;
rb.isKinematic = false;
remaininghits--;
StartCoroutine(Release ());
}
IEnumerator Release(){
AudioSource.PlayClipAtPoint (leave, transform.position);
yield return new WaitForSeconds (releaseTime);
GetComponent<SpringJoint2D> ().enabled = false;
this.enabled = false;
//New ball
yield return new WaitForSeconds (2f);
if (nextBall != null) {
nextBall.SetActive (true);
Destroy (gameObject, 3f);
}
else {
yield return new WaitForSeconds (loadtime);
remaininghits=10;
Application.LoadLevel ("start");
}
}
}
Answer by Harinezumi · Jan 12, 2018 at 02:32 PM
Hey!
I think what you are looking for is either instantiating a prefab from a control script, or resetting your object.
Instantiating prefab way:
Instead of having a GameObject nextBall, you should create a prefab from your ball object (a kind of template) and have a GameObject ballPrefab (this is just naming, but it kinda matters). In the current setup it would have a reference to itself, and in Release() you do
if (ballPrefab != null) {
Instantiate(ballPrefab); // optionally specify starting position and rotation as well
Destroy(gameObject, 3f);
}
Resetting your object The other approach is that instead of Destroy()-ing your gameObject, you just store at the beginning the initial position, etc. and reset it:
private Vector3 initialPosition;
void Start () {
initialPosition = transform.position;
}
IEnumerator Release() {
...
transform.position = initialPosition;
}
As a side note, your class does too many things, it has too many responsibilities, so it is difficult to add functionality to it and make it work exactly as you want to. For example, it does collision detection, input handling, object management, and even some UI thing based on the Text component it has.
I would only leave in the Ball script only the collision detection playing sounds, move the Update(), OnMouseDown(), OnMouseDown(), and Release() into a new BallThrowInput class, and the part with nextBall would go into a BallCount class, that has a reference to a ball prefab. This is just a rough sketch, some parts missing, but it would make your life a lot easier to separate these responsibilities.
Anyway, good luck!
Hey Friend Thanx for ur time the ball is instantiated but it not allows me to pull the ball it not allows the spring joint
Your answer
Follow this Question
Related Questions
Placing object with ray cast 3 Answers
GetAxisRaw() Ignoring Second Item 1 Answer
Optimizing Thousands of game objects... 1 Answer
Deleting all child clones in a GameObject 2 Answers
HELP 2D Game: Clone not picking up object (Crystals) HELP 1 Answer