- Home /
Issue with my Teleport Script
I'm trying to make a script that will allow me to click on a Collider (with mouse button) that will destroy my player and create it in another location. (using a prefab of the player)
The current script i have is based of 2 seperate scripts (that work fine). One is an push the button and the elevator goes up or down. The other is my Killzone for when players fall down the level and it will respawn them at the start of the level.
#pragma strict
private var pressedButton : boolean = false;
private var teleportup : boolean = false;
var Player : GameObject;
var location2 : Transform;
function OnTriggerEnter2D(other : Collider2D){
pressedButton = true;
}
function OnTriggerExit2D(other : Collider2D){
pressedButton = false;
}
function OnMouseDown(){
if(teleportup == true){
Destroy(other.gameObject);
var P : GameObject = Instantiate(Player, location2.position, Quaternion.identity);
}
}
function OnGUI()
{
if(pressedButton == true){
GUI.Box(new Rect(300,300,200,20), "Click to Activate!");
}
}
The error i'm currently getting is: teleporter.js(20,25): BCE0005: Unknown identifier: 'other'.
I've tried adding function OnMouseDown(other : Collider2D), trying to identify the "other" but that doesn't seem to work. It allows me to go ingame with that though, but when i click on the collider i get the following error:
Failed to call function OnMouseDown of class click teleporter Calling function OnMouseDown with no parameters but the function requires 1. UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)
My knowledge of programming is limited and i have alot of learning ahead of me, so any help would be appriciated.
Answer by supernat · Mar 28, 2014 at 01:17 AM
On line 20, if you want to destroy the player, it would be
Destroy(Player);
This is a function scope issue. The variable "other" doesn't exist in the function. The method OnMouseDown() is not your own method. You are overriding the method from a base class, so you can't just change what gets passed to it. When you change the function parameters, it becomes a different, unique, function. I would recommend brushing up on some programming basics online that will cover these. I like lynda.com myself, great videos there. Good luck.
Your answer
Follow this Question
Related Questions
Big noob Need to know how I can make that 1 Answer
Collision with renderer.enabled? 0 Answers
Creation of Edge Colliders at Runtime 0 Answers
Take boolean from other script error 1 Answer
Script doesn't find other script 0 Answers