unity3d - [Unity][Javascript] Why are these simple js scripts not working in Unity? -
i've been sort of teaching myself , sort of learning jimmy vegas on youtube: https://www.youtube.com/channel/ucrmxhq2rj9_0chs7mhl7erg
if haven't seen tutorials or don't want look, 1 of things create small script destroys coin when player collider hits it, mine isn't working. code below (a little mis-formatted, sorry, couldn't format correctly):
function oncollisionenter (collision : collision) { if(collision.gameobject.tag == "coincollect") { destroy(this.gameobject); } }
i applied script prefab , placed bunch of coins around little area, additionally, made capsule collider in first person controller tagged "coincollect", , ticked "is trigger"
also, i'm trying make teleporter teleports first person character 1 teleporter another. code below:
var warptarget001 : gameobject; var warptarget002 : gameobject; function ontriggerenter (col : collider) { if (col.gameobject.tag == "warp001") { this.transform.position = warptarget002.position; } else if (col.gameobject.tag == "warp002") { this.transform.position = warptarget001.position; } }
i have 4 objects here, 2 warp pads , 2 warp targets. 2 warp pads tagged "warp001" , "warp002", respectively , 2 warp targets not assigned in code, assigned dragging , dropping empty object serialized field script provides. both pads have capsule colliders with "is trigger" unticked doesn't work either way, ticked or unticked.
can tell me might doing wrong? thank you.
the script correct, problem parent "fpscontroller" object didn't have rigidbody applied , should object (as opposed "firstpersoncharacter" object had nested inside of it) scripts applied to. seemed fix problem.
the correct code is:
/* coincollect.cs */ using unityengine; using system.collections; using unityengine.ui; public class coincollect : monobehaviour { private int _score; [serializefield] private text _text; void ontriggerenter ( collider collision ){ if(collision.gameobject.tag == "coin"){ destroy(collision.gameobject); _score++; _text.text = "score: " + _score; } } }
and:
/* warp.js */ var warptarget001 : gameobject; var warptarget002 : gameobject; function ontriggerenter (col : collider) { if (col.gameobject.tag == "warp001") { this.transform.position = warptarget002.transform.position; } if (col.gameobject.tag == "warp002") { this.transform.position = warptarget001.transform.position; } }
Comments
Post a Comment