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");
       }     
 
 }      
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
Use Get$$anonymous$$eyDown() to instantiate as suggested by alaus88, and change your mechanic so the animation and particle are separate.
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
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.
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");
         }
 
 }
 #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.
So just put the presskeyonce before the instantiate ins$$anonymous$$d
I changed my answer to my suggestion
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                