Hi everyone I made the 2 codes but it seems like they dont work. I made a PickUp code for my player and a PickUpFlash code in my flashlight. If anyone can help out by noticing whats wrong please let me know.
Here is the the code I put into the Character:
using UnityEngine;
using System.Collections;
public class PickUp : MonoBehaviour
{
public static GameObject WalkedOverObject;
void OnTriggerEnter(Collider other) {
WalkedOverObject = other.gameObject;
}
void OnTriggerExit(Collider other) {
if (WalkedOverObject == this.gameObject) WalkedOverObject = null;
}
}
Here is the the code I put into the Flashlight:
using UnityEngine;
using System.Collections;
public class PickUpFlash : MonoBehaviour {
void Update () {
if(Input.GetKeyDown(KeyCode.E))
{
if (PickUp.WalkedOverObject) Destroy(PickUp.WalkedOverObject);
}
}
}
I have no idea what is wrong, but here’s a checklist of some sorts:
The object you must trigger with, needs a trigger collider.
The player needs a character controller or a rigidbody with a collider.
The collision layers must be set so they trigger each other. (On by default)
The game objects and colliders/triggers needs to be enabled.
The collider and trigger must actually touch (maybe it’s too low/too high away from player?)
The scripts must be attached to the correct objects in the scene.
And I probably missed a few corner cases. But if all else is set up, your script seems to do the following:
If the Character enters any kind of trigger, remember that as the WalkedOverObject.
If the Character leaves itself (out of body experience? :-)), forget WalkedOverObject.
If “E” is pressed, and you haven’t WalkedOverObject, nothing happens.
If “E” is pressed, and you have WalkedOverObject, the object is Destroyed (which can be any trigger). I don’t know if it’s supposed to be a Flashlight or an Area nor how Destroying it would resemble the behaviour of a flashlight.
I can tell you one thing that this will do. It will not turn a single light on, based on this information alone. You are only destroying whatever you’ve bumped into when you press “E”.
Maybe you can start by telling if any of this behaviour is wrong and what part of this you need help with?