Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

spawning instantiated prefab on touch after he had been destroyed

Discussion in 'Scripting' started by Radiators, Aug 21, 2020.

  1. Radiators

    Radiators

    Joined:
    Aug 13, 2020
    Posts:
    29
    Hello
    i did a script which can on one touch spawn object (instantiated prefab), maximum amount of this objects in game is 3 and when they are not visible they will be destroyed, but i need these prefabs to be able spawn again after they had been destroyed

    I tried this but it does not work:

    Code (CSharp):
    1. public class CreatingCan: MonoBehaviour //This Script is added to gameObject canCreator in hiearchy
    2. {
    3.  
    4.     public GameObject Can;
    5.     public int numberOfCans;
    6.     public bool canSpawn = true;
    7.  
    8.     private void Start()
    9.     {    
    10.         numberOfCans = 0;
    11.     }
    12.     void Update()
    13.     {
    14.         if (Input.touchCount > 0)
    15.         {        
    16.             Touch touch = Input.GetTouch(0);
    17.             Vector2 TouchPosition = Camera.main.ScreenToWorldPoint(touch.position);
    18.             if (touch.phase == TouchPhase.Began && canSpawn == true)
    19.             {    
    20.                 if (Instantiate(Can, TouchPosition, Quaternion.identity))  
    21.                 {
    22.                     numberOfCans++;              
    23.                 }
    24.             }
    25.         }
    26.  
    27.         if (numberOfCans >= 3)
    28.         {
    29.             canSpawn = false;
    30.         }
    31.  
    32.     }
    33.  
    34. }
    Code (CSharp):
    1.  
    2. public class fallingCan : MonoBehaviour //THIS SCRIPT IS ADDED TO PREFAB
    3. {
    4. CreatingCan CC;
    5. private void Update()
    6. {
    7.     if (transform.position.x < Camera.main.transform.position.x - 10)
    8.     {
    9.         Destroy(gameObject);
    10.            CC.numberOfCans--;    
    11.     }  
    12. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    This is not super helpful.

    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220

    Help us to help you.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    As it is you never reset the canSpawn variable when the number of cans is reduced.
    Try replacing
    Code (CSharp):
    1.         if (numberOfCans >= 3)
    2.         {
    3.             canSpawn = false;
    4.         }
    with
    Code (CSharp):
    1. canSpawn = numberOfCans < 3;
     
  4. Radiators

    Radiators

    Joined:
    Aug 13, 2020
    Posts:
    29
    i can spawn 3 objects but then when they are destroyed i cannot spawn this objects again and also i get error message "Destroying assets is not permitted to avoid data loss"