Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by husnain_rao · Jan 12, 2018 at 01:51 PM · gameobjectsgenerateautomaticallyspring joint

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");
         }    
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image husnain_rao · Jan 14, 2018 at 05:03 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

74 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges