x


Pick Up Coins

Hi guys, i tried writing a script to pick up coins so that my score goes up and the coins dissapear but now when i try my main character (named gamer) runs again the coins and keeps running but nothing happens, score doesn't go up or coins don't dissapear, someone knows a script that i could use,

ty in advance.

more ▼

asked Apr 03 '12 at 06:45 PM

starvar gravatar image

starvar
58 12 17 19

Post your script and let us help you with what you've got, it's better to learn from your mistakes than getting a finished script that doesn't match your game.

Apr 03 '12 at 06:47 PM save

static var scoreText:String = "TotalScore: ";

static var score = 0;

function Update ()

{

scoreText = "TotalScore: " + score; 

}

function OnTriggerEnter( other : Collider ) {

Debug.Log("OnTriggerEnter() was called");

if (other.tag == "coin") {

    score += 5;

    scoreText = "TotalScore: " + score;

}

}

function OnGUI () {

GUI.Label (Rect (10, 50, 100, 20), scoreText);

}

------CODE----- here it is

Apr 03 '12 at 06:56 PM starvar
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Your code should work so let's check what could be the problem.

  1. coin does not enter trigger
  2. trigger does nothing

So first look that your debug message does get displayed.

  print("OnTriggerEnter() was called");

If that did not get called check the following:

  1. Coin has a collider/character controller attached to it, and it's enabled
  2. Player has a WORKING collider attached to it which is a TRIGGER
  3. check this with a box collider, see that IsTrigger is set to true (in the inspector) tip: always use simple shapes for testing scripts
  4. If you use a mesh collider (which is probably a bit of a waste for a pickup trigger) it might not work out of the box, so check if it works. Note that a collider can not be something to collide with AND a trigger at the same time(google raycast vs trigger). For a mesh to work as a collider it needs to fulfill certain requirements, it will not work out of the box if all it's faces are not convex. Go to the models import setting, and click on Optimize Mesh(Not sure if it really does but I think that might fix some issues) and more importantly "convex". What "convex" does is, as far as I unterstand, it pretends that the model's faces are convex even if they aren't. Collision might be slightly off, the resulting mesh collider might for example ignore that a bowl has an inside but at least it will work. And for a trigger that's more than good enough.
  5. Check that you have attached your script to the player-object and that its activated. Check that it is running.

If your trigger get's succesfully entered and still nothing happens the problem is probably somewhere between your score-variable and it's output.

To make the coins disappear and check the score change:

if (other.tag == "coin") {
    Destroy(other);
    print(score);

}
  1. Check that you haven't done something like declaring the coins a trigger. I suspect you're using a character controller or such for the player and have no trigger attached to it. If this is the case add component-physics-box collider to the gamer-object, size it accordingly and click "istrigger".
more ▼

answered Apr 03 '12 at 11:12 PM

schaddem gravatar image

schaddem
90 1 4

Forgot the 'is triger' in my box collider, awesome work, ty for your time!

Apr 04 '12 at 06:50 AM starvar
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x297
x68
x44
x23
x12

asked: Apr 03 '12 at 06:45 PM

Seen: 950 times

Last Updated: Apr 04 '12 at 06:50 AM