Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I can´t erase things from my inventory!!!

Discussion in 'Getting Started' started by GustavoIbarra7Tec, Jun 5, 2019.

  1. GustavoIbarra7Tec

    GustavoIbarra7Tec

    Joined:
    Jun 5, 2019
    Posts:
    1
    (Sorry for my english) Basically I'm doing a basic game in which I'll base to do a more complex game. My "minigame" consists in panels with things. I have a PanelContador with buttons (associated with a script "CuentaManzanas")to add apples and pears to the PanelInventario. I can add things but I can't destroy them with the buttons in PanelContador for remove things. I hope if you could help me, I will be very grateful with you. Here's my scene:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ControlFrutasINventario : MonoBehaviour
    6. {
    7.     public List<GameObject> FrutasenInventario = new List<GameObject>();
    8.  
    9.     public void AñadirFruta(GameObject fruta)
    10.     {
    11.         FrutasenInventario.Add(fruta);
    12.     }
    13.  
    14.     public void QuitarFruta(GameObject fruta)
    15.     {
    16.         FrutasenInventario.Remove(fruta);
    17.     }
    18.  
    19.     public bool TengoFruta(GameObject fruta)
    20.     {
    21.         return FrutasenInventario.Contains(fruta);
    22.     }
    23. }
    24.  
    Code (CSharp):
    1. public class CuentaManzanas : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.  
    5.     public Text CantApples;
    6.     public int NumApples;
    7.     public Text CantPeras;
    8.     public int NumPeras;
    9.     public GameObject Apple;
    10.     public GameObject Pera;
    11.     public ControlFrutasINventario controlfrutasinventario;
    12.     public GameObject canvasInventario;
    13.  
    14.  
    15.     private void Start()
    16.     {
    17.         NumApples = 0;
    18.         NumPeras = 0;
    19.         Contador();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     public void PlusOne()
    24.     {
    25.         NumApples += 1;
    26.         NumPeras += 1;
    27.         Contador();
    28.         GameObject nuevamanzana = Instantiate(Apple);
    29.         nuevamanzana.transform.parent = canvasInventario.transform;
    30.         nuevamanzana.tag = "Apple";
    31.         nuevamanzana.name = "Apple" + NumApples;
    32.         controlfrutasinventario.AñadirFruta(nuevamanzana);
    33.         ManzanaaInventario();
    34.        
    35.  
    36.         GameObject nuevapera = Instantiate(Pera);
    37.         nuevapera.transform.parent = canvasInventario.transform;
    38.         nuevapera.tag = "Pear";
    39.         controlfrutasinventario.AñadirFruta(nuevapera);
    40.         PeraaaInventario();
    41.    
    42.     }
    43.  
    44.     public void minusOneApple()
    45.     {
    46.         NumApples -= 1;
    47.         Contador();
    48.        
    49.  
    50.        // Destroy(GameObject.FindGameObjectWithTag("Apple"));
    51.     }
    52.  
    53.  
    54.     public void minusOnePera()
    55.     {
    56.         NumPeras -= 1;
    57.         Contador();
    58.         controlfrutasinventario.QuitarFruta(Pera);
    59.     }
    60.  
    61.     private void Contador()
    62.     {
    63.      
    64.         if (NumApples < 0)
    65.         {
    66.             NumApples = 0;
    67.         }
    68.         if (NumPeras < 0)
    69.         {
    70.             NumPeras = 0;
    71.         }
    72.         CantApples.text = "Manzanas: " + NumApples;
    73.         CantPeras.text = "Peras: " + NumPeras;
    74.     }
    75.  
    76.     public void ManzanaaInventario()
    77.     {
    78.        
    79.     }
    80.  
    81.     public void PeraaaInventario()
    82.     {
    83.        
    84.     }
    85.  
    86.     public void BorraApple()
    87.     {
    88.         Destroy(Instantiate(Apple));
    89.     }
    90.  
    91.  
    92. }
    93.  
    upload_2019-6-4_19-50-55.png

    upload_2019-6-4_19-51-17.png
    upload_2019-6-4_19-51-49.png

    As you can see there are 0 apples and 0 pears, but I still have the objects in the CanvasInventario
     

    Attached Files:

  2. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Hi Gustavo (hope I got the name right...)

    Okay, I'm trying to figure out the code from the context... but think I see your problem.

    When you create an apple/pear you create a game object, place it on the canvas, and update the database (Inventario?) with the new fruit game object. However, when you delete an Apple or pear you have 2 problems.

    1)
    minusOneApple()
    never calls
    controlfrutasinventario.QuitarFruta(Pera);

    3) When you call
    controlfrutasinventario.QuitarFruta(GameObject fruta);
    you pass in the prefab. But you need to pass in a way for the list to find the right kind of object. Since you set the tag and name when you create the apple and pear object, I suggest you pass it a string for the name or tag instead of the prefab gameobject.
    2) Even if it did, all that
    controlfrutasinventario.QuitarFruta(GameObject fruta);
    does is remove take the 'reference' to the game object out of the list - it never deletes the actual game object itself.

    I hope this makes sense. If I get time later today I'll try to re-write your code to see if I can get it to do what you want... no promises though :(
     
  3. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Okay, these are the changes I'd make...


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ControlFrutasINventario : MonoBehaviour
    6. {
    7.     public List<GameObject> FrutasenInventario = new List<GameObject>();
    8.  
    9.     public void AñadirFruta(GameObject fruta)
    10.     {
    11.         FrutasenInventario.Add(fruta);
    12.     }
    13.  
    14.     public void QuitarFruta(string tagToDelete)
    15.     {
    16.         // First check to see if there is an object that matches the tag to be deleted
    17.         if(FrutasenInventario.Exists(x => x.CompareTag(tagToDelete)))
    18.             // There is one or more with the right tag.  Get the last one and destroy it.
    19.             Destroy(FrutasenInventario.FindLast(x => x.CompareTag(tagToDelete)));
    20.     }
    21.  
    22.     public bool TengoFruta(GameObject fruta)
    23.     {
    24.         return FrutasenInventario.Contains(fruta);
    25.     }
    26. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CuentaManzanas : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.  
    10.     public Text CantApples;
    11.     public int NumApples;
    12.     public Text CantPeras;
    13.     public int NumPeras;
    14.     public GameObject Apple;
    15.     public GameObject Pera;
    16.     public ControlFrutasINventario controlfrutasinventario;
    17.     public GameObject canvasInventario;
    18.  
    19.  
    20.     private void Start()
    21.     {
    22.         NumApples = 0;
    23.         NumPeras = 0;
    24.         Contador();
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     public void PlusOne()
    29.     {
    30.         NumApples += 1;
    31.         NumPeras += 1;
    32.         Contador();
    33.         GameObject nuevamanzana = Instantiate(Apple);
    34.         nuevamanzana.transform.parent = canvasInventario.transform;
    35.         nuevamanzana.tag = "Apple";
    36.         nuevamanzana.name = "Apple" + NumApples;
    37.         controlfrutasinventario.AñadirFruta(nuevamanzana);
    38.         ManzanaaInventario();
    39.  
    40.  
    41.         GameObject nuevapera = Instantiate(Pera);
    42.         nuevapera.transform.parent = canvasInventario.transform;
    43.         nuevapera.tag = "Pear";
    44.         controlfrutasinventario.AñadirFruta(nuevapera);
    45.         PeraaaInventario();
    46.  
    47.     }
    48.  
    49.     public void minusOneApple()
    50.     {
    51.         NumApples -= 1;
    52.         Contador();
    53.         controlfrutasinventario.QuitarFruta("Apple");
    54.     }
    55.  
    56.  
    57.     public void minusOnePera()
    58.     {
    59.         NumPeras -= 1;
    60.         Contador();
    61.         controlfrutasinventario.QuitarFruta("Pear");
    62.     }
    63.  
    64.     private void Contador()
    65.     {
    66.  
    67.         if (NumApples < 0)
    68.         {
    69.             NumApples = 0;
    70.         }
    71.         if (NumPeras < 0)
    72.         {
    73.             NumPeras = 0;
    74.         }
    75.         CantApples.text = "Manzanas: " + NumApples;
    76.         CantPeras.text = "Peras: " + NumPeras;
    77.     }
    78.  
    79.     public void ManzanaaInventario()
    80.     {
    81.  
    82.     }
    83.  
    84.     public void PeraaaInventario()
    85.     {
    86.  
    87.     }
    88.  
    89.     public void BorraApple()
    90.     {
    91.         Destroy(Instantiate(Apple));
    92.     }
    93.  
    94.  
    95. }

    PLEASE NOTE: I have not tested this... make sure that you backup you work before you try and use this.

    The only problem this will leave is that the inventory won't be nice and neat...