how to set a value in one script and call it in another? (c#)

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 Traphas 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)

I’m actually suprised that you used and Ienum with the Start function I didn’t even know that was possible but what I would do is simplify it to 1 script yep 1 that’s it.

Somethings to keep track of are A. Make sure your script is on a object with a collider set as isTrigger or it won’t be called. B. Why I hate using tags I always forget to set them on the object in the inspector so make sure your player is set with a player tag. Finally make sure your player has a rigidbody as well or the trigger wont run.

C. this is just me being a whiney programmer you don’t gotta do this it would just be less lines of code.
Don’t program an animation create an animation and play it in code (MoveObject)

Here is a good starter for you

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

[RequireComponent(typeof(BoxCollider))]
public class Trap : MonoBehaviour 
{
	public Vector3 point2;
	
	public bool isTrapEnabled;
	public bool shouldResetPlayer;

	void OnTriggerEnter(Collider other)
	{
		if(other.tag == "Player")
		{
			if(shouldResetPlayer)
			{
				SceneManager.LoadScene(SceneManager.GetActiveScene().name);
			}
			else
			{
				StartCoroutine(PingPong());
			}
		}
	}

	IEnumerator PingPong()
	{
		//Move object back and forth
		yield return null; //waits till next frame
	}
}