Search Unity

Gauntlet Item Pickup

Discussion in 'Scripting' started by Velketor, Mar 30, 2008.

  1. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Hello,

    I want my character to be able to run into a game object and gain +1. Example: 1/50, 2/50, 3/50, etc depending on how many objects they run into.

    Does anyone know of a script that can do this for me? I know I need a GUI Text or GUI Texture to visibly show the player how many objects they have run into--I just don't know very much about scripting or GUI implementation.

    Can anyone explain this to me please? Thanks so much!

    --Velk
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    this sounds pretty much like the healthpack arrangement thats in the FPS tutorial, have you checked that out?

    HTH
    AC
     
  3. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Thanks for the response!

    I have been working closely with the FPS tutorial--but I still can't get it to work. I downloaded/modified the example FPS project and it seems like their scripts have a lot of extra stuff that I do not need. The problem is, I'm terrible at scripting and whenever I delete lines of code, I get lots and lots of error messages.

    Whenever you shoot the machine gun in the FPS tutorial, the GUIText number goes down from "50" in increments of 1 until it reaches 0. I'd like that same type of functionality, except I want the number to INCREASE from 0 to 50 by an increment of 1 each time the player collides with a game object.

    Can anyone help me out with some code that I actually need for this to work? I'll keep on working for now =)

    Thanks everyone, your help is always appreciated!

    --Velk
     
  4. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    You have go the version that has 3 healthpacks floating in the first bay, right? thats the scripts Im referring you to. They can be switched to ammo or health, and they increase the relevant count in your gui. Have a good fiddle with them.

    Sorry if thats no help. I'll leave this thread alone now.

    AC
     
  5. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    Try this:

    You need an object with a collider. The collider needs to be a trigger. To do this, you just check the IsTrigger property in the inspector.

    Create a Script and drag it on to the object. Edit the script to create a Debug message OnTriggerEnter:

    Code (csharp):
    1.  
    2. function OnTriggerEnter (other){
    3.    Debug.Log("You walked into me");
    4. }
    5.  
    Test that out. When you walk into the object, you should get a Debug message letting you know.

    Ok, so let's expand the script tell us how many times you have walked into the item:

    Code (csharp):
    1.  
    2. var times = 0;
    3. function OnTriggerEnter(other){
    4.    times += 1;
    5.    Debug.Log("You walked into me "+times+" times.");
    6. }
    7.  
    Does that make sense?
    Now when you test it, our variable times, which we initially set to 0, increases by 1.

    So, here is the thing -- if you duplicate the object that you are colliding with, each object will keep a unique count of collisions. This makes sense, but probably isn't the effect that you wanted.

    Can you think of a solution to this?
    Here is a hint: look in the ScriptReference at Variables in the Overview.