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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to make Pickups / Collectables only able to be picked up once?

Discussion in 'Scripting' started by iEpic, May 28, 2014.

  1. iEpic

    iEpic

    Joined:
    Sep 29, 2013
    Posts:
    119
    I have some rocket boost power ups in my 2D game but I want the player to only be able to pick them up once.
    I have the number of rockets saved in the playerprefs so their rockets transition through scenes but I don't want them to be able to just keep reloading the same scene and keep getting a million rockets.
    How could I go about making them only be picked up once??
    Thanks for any help!
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Flag your pickup with an ID. If the playerpref contains the ID already, deactivate the item on load.
     
  3. iEpic

    iEpic

    Joined:
    Sep 29, 2013
    Posts:
    119
    Ok so would each pick up have its own individual ID?
     
  4. ZFarago

    ZFarago

    Joined:
    Aug 13, 2012
    Posts:
    154
    For the idea before, you can re-use some unique parameter the prefab / gameobject already have, like its name / material's name etc.
     
  5. MytsosR

    MytsosR

    Joined:
    May 23, 2014
    Posts:
    22
    i use this code :

    Code (csharp):
    1.  
    2. #pragma strict
    3. var item1 : Transform;
    4. var playerTransform : Transform;
    5. var totalItems = 0;
    6.  
    7.  
    8. function Update ()
    9. {
    10.     var distance1 = (item1.position - playerTransform.position).magnitude;
    11.    
    12.     if(distance1 <= 3)
    13.     {
    14.         if(Input.GetKeyDown("e"))
    15.         {  
    16.             totalItems += 1;
    17.             item1.position = Vector3(0, 0, 0);
    18.             Debug.Log("You collected one item. You have : " + totalItems + " items");
    19.         }
    20.     }
    21. }
    22.  
    when you press E it teleport the item to a place where is not visible by the map and i can't take it again
     
  6. iEpic

    iEpic

    Joined:
    Sep 29, 2013
    Posts:
    119
    yea but once you reload the scene won't it still be in the original place and you would be able to collect it again.
     
  7. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    LightStriker has the right answer. Each pickup needs a unique id. When you reload the scene, you need to see which unique ids the player has already collected, then deactivate/destroy those items as soon as the level loads.

    Alternatively, load everything at runtime and just don't load the items the player has already picked up.

    If you want to make sure the player isn't able to pick up that exact item again, there's no other way to do it than that exact item having a unique id. How else can we identify that specific item?