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 oneted_7 · Nov 23, 2019 at 01:54 PM · instantiateunity 2dcreate

How create ONLY a object ?!

Hey guys. i am trying build chekpoints for my game . everytime player getkeydown for 2 s , game create only a chekpoint (if his mana was enough ) ,but when i try create my chekpoint object ,with instantiate() ,unity build chekpoints over and over untill my codintion will be wrong(mana>= 20 only work ,i think process is so fast for this cpenough condintion will ignore). my code is this .(ac is a paramtere for knowing distance between chekpoints, player.gd is player on ground) let me know if u need more information . tnqs for your time :)

 void update()
     {
         ac = player.transform.position.x - levelManager.currentChekPoint.transform.position.x;
         if (ac > 3 || ac < -3)
             cpenough = false;
         else cpenough = true;
 
         //making chekpoint(cp)
         if (player.gd)
         {
             if (Input.GetKeyDown(KeyCode.E))
             { mcpcounter = mcp;
             }
             if (Input.GetKey(KeyCode.E))
             {
                 if (manamanager.mana >= 20 )
                 {
                     if (cpenough == false)
                     {
                         mcpcounter -= Time.deltaTime;
                         if (mcpcounter <= 0)
 
                         {
 
                             for (int i = 1; i <= 1; i++)
                             {
                                 manamanager.mana -= 10;
 
                                 mcpcounter = mcp;
                                 Instantiate(cp, player.transform.position, player.transform.rotation);
                                 levelManager.currentChekPoint.transform.position = cp.transform.position;
                                 cpenough = true;
                             }
                         }
                     }
                 }
             }
             if (Input.GetKeyUp(KeyCode.E))
                 mcpcounter = mcp;
         }
     }


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
0

Answer by TheVivi · Nov 23, 2019 at 02:34 PM

Input.GetKey gets called every frame try moving everything from Input.GetKey to the Input.GetKeyUp or Input.GetKeyDown like so:

 void Update () {
         if (Input.GetKeyUp (KeyCode.E)) {
             mcpcounter = mcp;
             if (manamanager.mana >= 20) {
                 if (cpenough == false) {
                     mcpcounter -= Time.deltaTime;
                     if (mcpcounter <= 0) {
                         for (int i = 1; i <= 1; i++) {
                             manamanager.mana -= 10;
                             mcpcounter = mcp;
                             Instantiate (cp, player.transform.position, player.transform.rotation);
                             levelManager.currentChekPoint.transform.position = cp.transform.position;
                             cpenough = true;
                         }
                     }
                 }
             }
         }
     }

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 oneted_7 · Nov 23, 2019 at 06:33 PM 0
Share

didnt work :( exatly dont work cuz if use getkeyup when key realse time.deltatime will decrease about 0.2 (a fram) and never gone to be 0 from 2.

avatar image TheVivi oneted_7 · Nov 23, 2019 at 06:47 PM 0
Share

I understand in that case you can keep it in the Get$$anonymous$$ey but you need to use boolean to say that only happens once like so:

 bool once;
 
     void Update () {
         ac = player.transform.position.x - level$$anonymous$$anager.currentChekPoint.transform.position.x;
         if (ac > 3 || ac < -3)
             cpenough = false;
         else cpenough = true;
 
         //making chekpoint(cp)
         if (player.gd) {
             if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E)) {
                 mcpcounter = mcp;
                 once = true;
             }
             if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.E)) {
                 if (manamanager.mana >= 20 && once) {
                     if (cpenough == false) {
                         mcpcounter -= Time.deltaTime;
                         if (mcpcounter <= 0)
                         {
                             for (int i = 1; i <= 1; i++) {
                                 if(once) {
                                     manamanager.mana -= 10;
                                     mcpcounter = mcp;
                                     Instantiate (cp, player.transform.position, player.transform.rotation);
                                     level$$anonymous$$anager.currentChekPoint.transform.position = cp.transform.position;
                                     cpenough = true;
                                     once = false;
                                 }
                             }
                         }
                     }
                 }
             }
             if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.E)) {
                 mcpcounter = mcp;
                 once = false;
             }
         }
     }
avatar image oneted_7 TheVivi · Nov 24, 2019 at 09:26 AM 0
Share

nope.exatly i did same with cpenough . unity alwayse create 3x object .first time 3 ,second 6 and 9 in next ....

Show more comments
avatar image
0

Answer by HenriMR · Nov 23, 2019 at 06:49 PM

I think you could use a Coroutine for that. Something like that:

 Coroutine coroutine =null;
 
 void Update()
 {
 
    if(Input.GetKeyDown(KeyCode.E))
     {
             if (manamanager.mana >= 20 )
              {
                  if (cpenough == false)
                  {
                       //do your stuff

                      if (coroutine != null) { StopCoroutine(coroutine); }
            
                     coroutine = StartCoroutine (Checkpoint());
                  }
              }
     }
 }
 
 private IEnumerator Checkpoint()
 {
      float t = 2;
 
      while (t>0)
      {
              yield return null;
 
               t -=Time.deltaTime;
 
               if (Input.GetKeyUp)
              {
                     yield break;
              }
       }
 
       //create check point
 }
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 oneted_7 · Nov 24, 2019 at 08:53 AM 0
Share

didnt help :(. i think yeild return use for doing something over and over with some time distance. it help if chek "ifs" but dont work.

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

145 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 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

A bit complicated instantiate question 2 Answers

How to drag an instantiated UI element? 0 Answers

Unity is automatically instantiating variables 1 Answer

Instantiated object not showing in scene or hierarchy 2 Answers

How should I create/instantiate a skill/enemy/item 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