• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

The problem with FindObjectOfType and Instantiate.Why is not one object created, but a multiplied copy? 1 Answer

How can I instantiate a different sprite depending on where Raycasting goes? 0 Answers

Spawn A Set Amount of Objects at Set Location 1 Answer

Create plane from 2 Vectors of a symmetry line segment 1 Answer

im trying to hold an object in front of the player in game 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges