Script 1 talking with script 2 C#

hello guys sorry to bother :stuck_out_tongue: but i am ripping out whatever hair i got left xD
i got a Door, with 2 colliders, 1 for each side, and then i got the animation on another object. so what i want is to get the door open on E script to read the bool on my collider so if character is touching collider, and hitting E it will play the “open door” animation
i have been searching though alot of things but simply cannot get anything to work… they work as 2 but not as 1 :confused:

this is the first one:

public class door_open : MonoBehaviour {


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	other.GetComponent(door_collider1).door_open1();
	
	if (Input.GetKeyDown("e")){
		Debug.Log ("e was pressed for collider 1");
	}
}

}

and the second one here:

public class door_collider1 : MonoBehaviour {
	public bool door_open1 = false;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
	
	}
	void OnTriggerEnter(Collider other) {
		door_open1 = true;

}
		void OnTriggerExit(Collider other) {
		door_open1 = false;

}
}

thanks alot in advance :confused: this really bothers me and i am very new to programming

what do you mean "they work as 2 but not as 1"?

–

4 Answers

4

If you want to open a door when:

  1. user press button E
  2. user is colliding with the door

1 script is all you need. Just attach it to the colliders. The setup I recommend is any door collider children of the door:

using UnityEngine;
using System.Collections;

public class ActivateDoor : MonoBehaviour {
	public Transform door; // select the door in the inspector
	private bool colidingThisDoor = false;
	
	void Start () {
		// select the parent as the door, as adviced, if it wasn't manually set
		if (door == null) {
			door = transform.parent;
		}
	}
	
	void OnTriggerEnter (Collider other) {
		colidingThisDoor = true;
	}
	void OnTriggerExit(Collider other) {
		colidingThisDoor = false;
	}
	
	void Update () {
		if (colidingThisDoor) {
			if (Input.GetKeyDown("e")){
				Debug.Log ("E was pressed for door "+ door.name +" on collider "+ transform.name);
			}
		}
	}	
}

In the OnTriggerEnter event a collider is passed in. In this case you call it other, use that to pass information between the two scripts.

public class DoorCollider : MonoBehaviour
{
   public bool openIn = true;

   void OnTriggerEnter(Collider other)
   {
      Door door1 = other.transform.GetComponent<Door>();
      door1.Open(openIn);
   }
}

public class Door : MonoBehaviour
{
   public void Open(bool openIn)
   {
      string anim = (openIn) ? "openIn" : "openOut";
      animation.CrossFade(anim);
   }
}

Then when you walk up to the trigger, the door will open in the direction you want.

also i want to keep the names i made, and make it 2 the way i set it up there, and be useable on multiple doors without them interupting eachother. thanks in advance

–

but i want it so that when i press E the door will open. not before, also i want this scriptt to be esued on multiple doors so you wont open ALL doors in hte entire level when you open 1. thanks alot though, but not exactly what i want

The above script is designed to be used with multiple doors. Put the Door class on as many doors as you like. Put the DoorCollider class on as many triggers as you like and on each side adjust the boolean in the editor to true/false depending which way you want the door to open.

As for having it only activate when you press E, that should be simple enough to do. Just check in OnTriggerEnter to see if the E button is down the same way you were before. The code below should work. Let me know if it doesn't.

void OnTriggerEnter(Collider other)
{
   if (Input.GetKeyDown("e")) 
   {
      Door door1 = other.transform.GetComponent();
      door1.Open(openIn);
   }
}

i was just dumb there for a few... fixed it now thanks alot man!

–

both you guys should use comments and edit... It's an encouraged practice in any Q&A site to usually (there are specific exceptions, depending on the topic) have 1 answer per user and make it an answer - unlike @Rakaens self "answer" there.

–