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 /
  • Help Room /
avatar image
0
Question by IKilledKenny_2 · Jan 09, 2016 at 03:20 PM · javascriptcollisiongameobjectcharacterspawn

Spawning only 1 gameObject

Hello Unity3D.I have a question about spawning?How can i make it that whenever i use the functions GetKey Or GetButton i only spawn 1 and only 1 gameObject?For example whenever i use GetButton For my Player He spawn Over hundreds of the same gameObject.I dont want that.I want him to spawn 1 and only 1 of the same gameObject.If anyone knows how i can make it that my player spawn 1 gameObject by using GetButton or GetKey.Can you please tell me how?

Heres What i got So Far #pragma strict var ChargeButton : String = "Charge_P1"; var ChargeEffect : GameObject; var ChargeSpawn : Transform; var numShots : float = 1;
private var anim: Animation;

  function Awake() {
     anim = GetComponent.<Animation>();
 }   
 
 
 
       function Start() {
    if (numShots / 1 * 1 == numShots) numShots++; // Need an odd number of shots
    if (numShots < 1) numShots = 1;  // At least 3 shots for a fan
  }
 
 
     
  function Update() {
     for(var i = 1; i < numShots; i++)
      if (Input.GetButton(ChargeButton)) {
          anim.Play("Charge");
          anim["Charge"].speed =.50;
          Instantiate(ChargeEffect,ChargeSpawn.transform.position,ChargeSpawn.transform.rotation);
            Debug.Log("User pressed C");
            }
            else{
            if(Input.GetButtonUp(ChargeButton))
            Destroy(ChargeEffect); 
            anim.Stop("Charge");
       }     
 
 }      
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by alaus88 · Jan 09, 2016 at 03:29 PM

use Input.GetKeyDown() instead.

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

Answer by IKilledKenny_2 · Jan 09, 2016 at 05:44 PM

This Doesn't really help because i need the animation and the particle to be looped and when i use GetKeyDown it makes the object and animation appear once

Comment
Add comment · Show 3 · 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 wyvernfist · Jan 09, 2016 at 07:36 PM 0
Share

Use Get$$anonymous$$eyDown() to instantiate as suggested by alaus88, and change your mechanic so the animation and particle are separate.

avatar image IKilledKenny_2 wyvernfist · Jan 09, 2016 at 10:34 PM 0
Share

That didn't work.It only spawn 1 of the particle and then it went away i only want 1 particle to show because my player is doing some kind of power up thing and i want him to spawn only 1 of the prefab and make it loop while holding down the charge button.But i already have the prefab particle looped

avatar image wyvernfist IKilledKenny_2 · Jan 10, 2016 at 09:35 AM 0
Share

Get$$anonymous$$eyDown() >>> Instantiate Input.GetButton(ChargeButton) >>> Animation

You don't have to have it all in one chunk, just make sure the Instantiate is called before animation.

avatar image
0

Answer by ClearRoseOfWar · Jan 10, 2016 at 03:19 AM

Simply use a bool.

 #pragma strict
 var ChargeButton : String = "Charge_P1";
 var ChargeEffect : GameObject;
 var ChargeSpawn  : Transform;
 var numShots : float = 1;
 var presskeyonce: boolean = false;
 private var anim: Animation;
 
 function Awake() {
     anim = GetComponent.<Animation>();
 }
 
 
 
 function Start() {
     if (numShots / 1 * 0 == numShots) numShots++; // Need an odd number of shots
     if (numShots < 1) numShots = 1;  // At least 3 shots for a fan
 }
 
 
 
 function Update() {
     for (var i = 1; i < numShots; i++)
         if (Input.GetButton(ChargeButton)) {
             anim.Play("Charge");
             anim["Charge"].speed = .50;
             if(presskeyonce == false) Instantiate(ChargeEffect, ChargeSpawn.transform.position, ChargeSpawn.transform.rotation);
             presskeyonce = true;
             Debug.Log("User pressed C");
         }
         else {
             if (Input.GetButtonUp(ChargeButton) && presskeyonce == true)
                 presskeyonce = false;
             Destroy(ChargeEffect);
             anim.Stop("Charge");
         }
 
 }

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 IKilledKenny_2 · Jan 10, 2016 at 04:10 AM 0
Share
 #pragma strict
 var ChargeButton : String = "Charge_P1";
 var ChargeEffect : GameObject;
 var ChargeSpawn  : Transform;
 var numShots : float = 1;  
 var presskeyonce:boolean = false;
 private var anim: Animation; 
  
  function Awake() {
     anim = GetComponent.<Animation>();
 }   
 
 
 
       function Start() {
    if (numShots / 1 * 0 == numShots) numShots++; // Need an odd number of shots
    if (numShots < 1) numShots = 1;  // At least 3 shots for a fan
  }
 
 
     
  function Update() {
     for(var i = 1; i < numShots; i++)
      if (Input.GetButton(ChargeButton)&& presskeyonce == false) {
          anim.Play("Charge");
          anim["Charge"].speed =.50;
          Instantiate(ChargeEffect,ChargeSpawn.transform.position,ChargeSpawn.transform.rotation);
          presskeyonce = true;
            Debug.Log("User pressed C");
            }
            else{
            if(Input.GetButtonUp(ChargeButton)&& presskeyonce == true)
            presskeyonce = false;
            Destroy(ChargeEffect); 
            anim.Stop("Charge");
       }     
 
 }   


This Isn't what i want either.the code what you have gave me makes my character spawns the prefab once and then after 2 seconds it stops spawning the prefab.I don't want that.I want the script to spawn only one prefab and loop the prefab do the animation that i have set as a loop animation as long as im holding the charge button down and as soon as i release the charge button both the prefab and the animation stops playing.

avatar image ClearRoseOfWar IKilledKenny_2 · Jan 10, 2016 at 04:39 AM 0
Share

So just put the presskeyonce before the instantiate ins$$anonymous$$d

I changed my answer to my suggestion

avatar image ClearRoseOfWar ClearRoseOfWar · Jan 10, 2016 at 04:56 PM 0
Share

Did that help?

Show more comments

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

55 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

Related Questions

Multiplayer Question 1 Answer

Multiplayer 2 Answers

Destroy Object on Collision (with di sound) 0 Answers

Soccer game, more realistic net 1 Answer

How to make a Compact-Disc shaped collider object with an empty hole inside? (2D, not a 3D torus) 0 Answers


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