How to enable a script in Play-Mode?
Hey there.
First of all I'm quite a noob, so please be patient with me....
I'm currrently working on a jump game quite similiar to Doodle Jump.
My problem is that I want to enable a specific script as soon as the game character passes the same line as where a platform is located. So it doesn't have tobe the case that the player gets in touch with the platform. I tried to get and compare the position coordinates of the two objects and then wanted to check if they are equal and if this was the case, the script should be enabled. I've spent lots of time with it already, but I still don't have a solution for this.
So please please help me!
Here is the code that should enable the other script:
using UnityEngine;
using System.Collections;
public class enableScript : MonoBehaviour {
private Transform othertransform;
private Transform player;
void Awake() {
float othertransform = GameObject.FindWithTag("MainCamera").transform.position;
float player = GameObject.FindWithTag ("Player").transform.position;
}
void Update () {
if (othertransform == player) {
(gameObject.GetComponent( "DestroyPlatform" ) as MonoBehaviour).enabled = true;
}
}
}
Then this is the code that should be enabled:
#pragma strict
var othertransform : Transform;
function Awake() {
othertransform = GameObject.FindWithTag("MainCamera").transform;
}
function Update () {
if (Vector3.Distance(transform.position, othertransform.position) > 20) {
Destroy(this.gameObject, 5);
}
}
and finally this is what the interface of the game looks like (the camera follows the player just on y-axis btw):
Thank you a lot for your help in advance!
Answer by Vice_Versa · Aug 25, 2015 at 06:58 PM
instead of "enabling" the script, you should just write the code you need into a function, then call that function when the player touches the area. what you can do for that is put a large invisible object in that area, make sure the colllider is on and isTrigger is checked, then in a script on that object, you can use OnTriggerEnter() to call that above mentioned function. make sense?
Edit: in your case actually what you should do is have a boolean variable set to false, then it gets set to true in the OnTriggerEnter function. Then have an if statement in the update function so that it wont execute that code unless the boolean variable is true.
firstly thank you for the response! secondly I think I understand what you mean and I tried to write the code like this but it still doesn't work. probably I made mistakes in the code. so here is it, maybe you can see what I did wrong:
#pragma strict
var DestroyPlatform : DestroyPlatform;
public var checkenter : boolean = false;
function Start () {
}
function Update () {
OnTriggerEnter();
if (checkenter == true) {
DestroyPlatform.Update();
}
}
function OnTriggerEnter () {
checkenter = true;
}
and the DestroyPlatform script is still the following:
#pragma strict
var othertransform : Transform;
function Awake() {
othertransform = GameObject.FindWithTag("$$anonymous$$ainCamera").transform;
}
function Update () {
if (Vector3.Distance(transform.position, othertransform.position) > 20) {
Destroy(this.gameObject, 5);
}
}
you dont call OnTriggerEnter in the update function, your game will automatically call that function when the objects overlap
Your answer
Follow this Question
Related Questions
Noob Here... Can you please give Script for moving a object into random Direction. 0 Answers
How to add a Type to a Class in Javascript? 1 Answer
OnCollisonEnter2D Not Firing after checking collider 1 Answer
Game development general question(Multiplayer - Singleplayer) 1 Answer
Running C# Function from JS file 0 Answers