here is the issue, i want to have 3 things happen, thus 3 scripts (if it can be done with fewer, that would be great). and it is not working like it should.
1. have the player trigger the `Trap`
2. have the trap be triggered by the `TrapTrigger` and start to move (an object)
3. have the player restart the level if they collide with the `Trap`
Tthe issue is, i cannot get it to work, the `TrapTrigger` does not activate the `Trap` to move. the `Trap` just starts on its own. I know this is because of the start function, but i can't use an update function with an `IEnumerator` . I was trying to do an `if` statement within `Start` to check if the `Trap`has been set active, but that doesn't seem to be working.
here are the 3 scripts.
- TRAP:
using UnityEngine;
using System.Collections;
public class Trap : MonoBehaviour
{
public Vector3 point2;
public Trap trap;
IEnumerator Start()
{
if (trap == enabled)
{
Vector3 point1 = transform.position;
while (true)
{
yield return StartCoroutine(MoveObject(transform, point1, point2, 2.0f));
yield return StartCoroutine(MoveObject(transform, point2, point1, 2.0f));
}
}
}
IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
float i = 0.0f;
float rate = 1.0f / time;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
}
}
- TRAP TRIGGER:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TrapTrigger : MonoBehaviour
{
public Trap trap { set; get; }
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
trap.enabled = true;
}
}
}
- RESTART:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ColRestart : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
SceneManager.LoadScene("Level_03");
}
}
}
all i want is the player to collide with the `TrapTrigger` - the `TrapTrigger` to start the `Trap` movement - and if they player enters the `Trap`, it triggers the restart script `ColRestart`.
any help is welcomed. thank you.
P.S. i did post this question before and though it was working, but after some testing it did not work as intended.
Please let me know if you need any more info.
- NOTES
- I was thinking to try having the
`TrapTrigger` on the `Trap` object,
but that would defeat the purpose, as
the game would just simply restart
because of the restart script.
- Think of it like an dart trap in a
puzzle game, like tomb raider, where
you step on a tile, and then darts
just shoot back and forth, and if the
player gets hit they take damage. in
this case if they get hit by the
object the game restarts.
- This is the error that comes up when I enter the `TrapTrigger` object collider. `NullReferenceException: Object reference not set to an instance of an object
TrapTrigger.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/TrapTrigger.cs:14) `
↧