In my game . I have added collectables , I have added a collectable naming “PickUp3” . I want it to work such that as the player touches it , the path to Level 2 naming “level2path” becomes active . Here is my script but I don’t know why it isn’t working . Help please .
Transcript :
using UnityEngine;
using System.Collections;
public class lev2 : MonoBehaviour {
void Start ()
{
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name == ("PickUp3"))
{
{
other.gameObject.name==("level2path")
{
other.gameObject.SetActive (true);
}
}
}
}
void Update () {
}
}
There are several things that can be the issue here.
First of all, to what object is this script attached? You would need this script to be on the player for it to pass the if-statement.
Second, does one of the objects (the player or the pickup object) have a rigidbody attached? And have you checked one of the colliders to be a trigger? If not then the OnTriggerEnter method will not be called.
Third, you are calling upon the variable ‘other’, but you don’t seem to have defined it for as far as I can see. Because of this it will not do anything with your object.
If I were you I would assign this script to the pick-up object instead of the player object, and check for a collision with the player to initiate the reaction. This would probably look something like this:
using UnityEngine;
using System.Collections;
public class Pickup3Script : MonoBehaviour {
public GameObject level2path;
void OnTriggerEnter (Collider col) {
if (col.gameObject.tag == "Player") {
level2path.SetActive (true);
}
}
}
In this case you would have tagged your player object with the standard tag “Player”, and you would have to drag the level2path object into the inspector. An alternative to having to insert it in the inspector would be to replace
level2path.SetActive (true);
with
GameObject.Find("level2path").SetActive (true);
In this case the path object should be named level2path.
Hope this helps.
Cheers!