• 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 sjdmengal · Feb 28, 2018 at 07:11 AM · instantiate-objects

how to instantiate objects for single time

how i am making a game where there is a ring which goes back and forth in the x direction and when i press a key i falls down now i want to instantiate another ring but it instantiates but clones the object 4 times i want it for one time

below is my code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RingController : MonoBehaviour {
  Rigidbody rb;
     public GameObject ring;
 
     private float Ringtransition=0.0f;
     private float Ringspeed = 2.5f;
     private const float Boud_size = 6.5f;
 
 
     // Use this for initialization
     void Awake(){
         rb = GetComponent<Rigidbody> ();
     }
 
     void Start () {
         InvokeRepeating ("MoveRing", 0.019f, 0.019f);
 
     }
 
 
     void MoveRing(){
         Ringtransition += Time.deltaTime * Ringspeed;
         float Xposition = Mathf.Sin (Ringtransition) * Boud_size;
         rb.transform.localPosition= new Vector3 (Xposition,6,0);
 
     }
 
     // Update is called once per frame
     void Update ()
     {
         if (Input.anyKey) {
             CancelInvoke ("MoveRing");
             rb.useGravity = true;
             Instantiate (ring, -ring.transform.localPosition,Quaternion.identity);
 
         }
     }
 
 
 }

 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by dishant27 · Feb 28, 2018 at 08:15 AM

Your instantiation will occur on every frame till any key is pressed down. Please try using Input.anyKeyDown, it is only called for the 1st frame when you press any key.

Comment
Add comment · Show 4 · 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 sjdmengal · Feb 28, 2018 at 08:51 AM 0
Share

Thanks for the help but it did not work.

avatar image ShadyProductions sjdmengal · Feb 28, 2018 at 09:02 AM 0
Share

is it still instantiating multiple rings? Perhaps it's also a better idea, to put the update method on 1 object that you can use as gamemanger, because I believe that the ringcontroller class is on the ring gameobject, so when you create a new ring object it will also have this script, which now means you have 2 ringscripts with both checking for the update trigger, so pressing anykey will trigger twice now. and create multiple rings. and so on..

avatar image Xpartano sjdmengal · Feb 28, 2018 at 09:10 AM 0
Share

Wow this sounds weird. Does your ring game object contain more than one ring? Using Input.any$$anonymous$$eyDown should instantiate just one ring (as @dishant27 says).

avatar image dishant27 sjdmengal · Feb 28, 2018 at 09:39 AM 0
Share

Perhaps, in that case, you can use a boolean check: using System.Collections; using System.Collections.Generic; using UnityEngine; public class RingController : $$anonymous$$onoBehaviour { Rigidbody rb; public GameObject ring; private float Ringtransition=0.0f; private float Ringspeed = 2.5f; private const float Boud_size = 6.5f; bool canInstantiate =true;

 // Use this for initialization 
 void Awake()
 { 
 rb = GetComponent<Rigidbody> (); 
 }
 
 void Start () 
 { 
 InvokeRepeating ("$$anonymous$$oveRing", 0.019f, 0.019f); 
 } 
 
 void $$anonymous$$oveRing()
 { 
 Ringtransition += Time.deltaTime * Ringspeed; 
 float Xposition = $$anonymous$$athf.Sin (Ringtransition) * Boud_size; rb.transform.localPosition= new Vector3 (Xposition,6,0); 
 } 
 
 // Update is called once per frame 
 void Update () 
 { 
 if (Input.any$$anonymous$$eyDown && canInstantiate) 
 { 
 CancelInvoke ("$$anonymous$$oveRing"); 
 rb.useGravity = true; 
 Instantiate (ring, -ring.transform.localPosition,Quaternion.identity); canInstantiate = false; 
 Invoke("CanInstantiate$$anonymous$$ethod", 0.5f); 
 } 
 } 
 
 void CanInstantiate$$anonymous$$ethod() 
 { 
 canInstantiate = true; 
 } 
 }
avatar image
0

Answer by sjdmengal · Mar 02, 2018 at 08:01 AM

Thank you all guys but i still have the same problem.

@ShadyProductions stated my problem well but i don't know how to solve it as he says when i press the key for 1 time it instantiates 1 ring and then 2 then 4,8 so on.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

76 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 avatar image avatar image

Related Questions

Cycle trought array of objects 1 Answer

Code generating 2 different results? 1 Answer

Box colliders getting Stuck 1 Answer

Instantiate an object in either x position A or B (not in between) 1 Answer

Smartest way to Instantiate different objects on different times. 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