1

I've been sort of teaching myself and sort of learning from Jimmy Vegas on youtube: https://www.youtube.com/channel/UCRMXHQ2rJ9_0CHS7mhL7erg

If you haven't seen those tutorials or don't want to look, one of the things he does is create a small script that destroys a coin when the player collider hits it, but mine isn't working. Code below (a little mis-formatted, sorry, couldn't get it to format correctly):

function OnCollisionEnter (collision : Collision) {
  if(collision.gameObject.tag == "coinCollect") {
    Destroy(this.gameObject);
  } 
}

I applied the script to a prefab and placed a bunch of coins around a little area, additionally, I made a capsule collider in a first person controller tagged "coinCollect", and ticked "Is Trigger"

Also, I'm trying to make a teleporter that teleports the first person character from one teleporter to 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 four objects here, two warp pads and two warp targets. The two warp pads are tagged "warp001" and "warp002", respectively and the two warp targets are not assigned anything in the code, but assigned by dragging and dropping an empty object into the Serialized Field the script provides. Both pads have capsule colliders with "Is Trigger" unticked but it doesn't work either way, ticked or unticked.

Can anyone tell me what I might be doing wrong? Thank you.

5
  • technical note: in JavaScript, == is for coerced equality, === for typed equality. However, what you're showing does not appear to be JavaScript, since JavaScript has no explicit typing. Is this TypeScript, or ActionScript, or some other variant? Because tagging your question with the correct language tag is going to get you better answers. Commented Aug 25, 2015 at 2:21
  • I copied what the youtuber had, warp script can be found here: weebly.com/uploads/4/7/6/0/47606749/warp_script.txt Commented Aug 25, 2015 at 2:50
  • @Mike'Pomax'Kamermans Javascript is the correct tag. This is a Unity3d variant. Unity3D refers to this as javascript in all of their resources. Commented Aug 25, 2015 at 5:15
  • Have you tried the coin one without IsTrigger ticked? Commented Aug 25, 2015 at 6:58
  • @Reasurria: it is often called unityscript, this is also the name of the stackoverflow tag. Commented Aug 25, 2015 at 11:27

2 Answers 2

1

The script was all correct, my problem was that my parent "FPSController" object didn't have a Rigidbody applied to it and should be the only object (as opposed to the "FirstPersonCharacter" object I had nested inside of it) that the scripts are applied to. That seemed to fix the 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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The only thing I can think of for your first problem is that it shouldn't need IsTrigger ticked. Other than that, it sounds like it should work (unless I'm missing something).

For the second problem you're having (with the warps), I don't think you can use warptarget001 by dragging and dropping objects into the fields. The reason being that what you've dragged into that field isn't the same object instance that's inworld.

You should assign their values through the code (preferably in the Start method), by using GameObject.Find("name") for example. This way, warptarget001 corresponds to the inworld gameobject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.