How to move OnTriggerEnter to Update
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class attacking : MonoBehaviour {
public GameObject exampleOne;
public GameObject exampleTwo;
public GameObject exampleThree;
public GameObject exampleFour;
public Camera camera;
public Camera camera2;
private float THEREHealth = 100;
private float THEREHealth2 = 100;
private float THEREHealth3 = 100;
private float THEREHealth4 = 100;
public Text enemyTEXT;
public Text enemyTEXT2;
public Text enemyTEXT3;
public Text enemyTEXT4;
void Start ()
{
camera.enabled = true;
camera2.enabled = false;
exampleOne.name = "N1";
exampleTwo.name = "N2";
exampleThree.name = "N3";
exampleFour.name = "N4";
}
void Update ()
{
setenemyTEXT ();
setenemyTEXT2 ();
setenemyTEXT3 ();
setenemyTEXT4 ();
if (camera2.enabled == false)
{
enemyTEXT.enabled = false;
enemyTEXT2.enabled = false;
enemyTEXT3.enabled = false;
enemyTEXT4.enabled = false;
}
if (THEREHealth < 0.1)
{
THEREHealth = 0;
}
if (THEREHealth2 < 0.1)
{
THEREHealth2 = 0;
}
if (THEREHealth3 < 0.1)
{
THEREHealth3 = 0;
}
if (THEREHealth4 < 0.1)
{
THEREHealth4 = 0;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "N1")
{
camera.enabled = false;
camera2.enabled = true;
setenemyTEXT ();
enemyTEXT.enabled = true;
if (Input.GetKeyDown(KeyCode.P))
{
THEREHealth = THEREHealth - 20;
}
if (THEREHealth == 0)
{
camera.enabled = false;
camera2.enabled = true;
Destroy (exampleOne);
}
}
if (other.gameObject.name == "N2")
{
camera.enabled = false;
camera2.enabled = true;
setenemyTEXT2 ();
enemyTEXT2.enabled = true;
if (Input.GetKeyDown(KeyCode.P))
{
THEREHealth2 = THEREHealth2 - 20;
}
if (THEREHealth2 == 0)
{
camera.enabled = false;
camera2.enabled = true;
Destroy (exampleTwo);
}
}
if (other.gameObject.name == "N3")
{
camera.enabled = false;
camera2.enabled = true;
setenemyTEXT3 ();
enemyTEXT3.enabled = true;
if (Input.GetKeyDown(KeyCode.P))
{
THEREHealth3 = THEREHealth3 - 20;
}
if (THEREHealth3 == 0)
{
camera.enabled = false;
camera2.enabled = true;
Destroy (exampleThree);
}
}
if (other.gameObject.name == "N4")
{
camera.enabled = false;
camera2.enabled = true;
setenemyTEXT4 ();
enemyTEXT4.enabled = true;
if (Input.GetKeyDown(KeyCode.P))
{
THEREHealth4 = THEREHealth4 - 20;
}
if (THEREHealth == 0)
{
camera.enabled = false;
camera2.enabled = true;
Destroy (exampleFour);
}
}
}
void setenemyTEXT ()
{
enemyTEXT.text = "life:" + THEREHealth.ToString ();
}
void setenemyTEXT2 ()
{
enemyTEXT2.text = "life:" + THEREHealth2.ToString ();
}
void setenemyTEXT3 ()
{
enemyTEXT3.text = "life:" + THEREHealth3.ToString ();
}
void setenemyTEXT4 ()
{
enemyTEXT4.text = "life:" + THEREHealth4.ToString ();
}
}
when i have the game run the items in OnTriggerEnter do not work, i assume because there not in void update. so how can i move OnTriggerEnter to Update without breaking the code?
Answer by TBruce · May 01, 2016 at 06:53 PM
This should fix your problem
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class attacking : MonoBehaviour
{
public GameObject exampleOne;
public GameObject exampleTwo;
public GameObject exampleThree;
public GameObject exampleFour;
public Camera camera;
public Camera camera2;
private float THEREHealth = 100;
private float THEREHealth2 = 100;
private float THEREHealth3 = 100;
private float THEREHealth4 = 100;
public Text enemyTEXT;
public Text enemyTEXT2;
public Text enemyTEXT3;
public Text enemyTEXT4;
private bool PKeyPressed = false;
void Start ()
{
camera.enabled = true;
camera2.enabled = false;
exampleOne.name = "N1";
exampleTwo.name = "N2";
exampleThree.name = "N3";
exampleFour.name = "N4";
}
void Update ()
{
PKeyPressed = Input.GetKeyDown(KeyCode.P);
setenemyTEXT ();
setenemyTEXT2 ();
setenemyTEXT3 ();
setenemyTEXT4 ();
if (camera2.enabled == false)
{
enemyTEXT.enabled = false;
enemyTEXT2.enabled = false;
enemyTEXT3.enabled = false;
enemyTEXT4.enabled = false;
}
if (THEREHealth < 0.1)
{
THEREHealth = 0;
}
if (THEREHealth2 < 0.1)
{
THEREHealth2 = 0;
}
if (THEREHealth3 < 0.1)
{
THEREHealth3 = 0;
}
if (THEREHealth4 < 0.1)
{
THEREHealth4 = 0;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "N1")
{
camera.enabled = false;
camera2.enabled = true;
setenemyTEXT ();
enemyTEXT.enabled = true;
if (PKeyPressed)
{
THEREHealth = THEREHealth - 20;
}
if (THEREHealth == 0)
{
camera.enabled = false;
camera2.enabled = true;
Destroy (exampleOne);
}
}
if (other.gameObject.name == "N2")
{
camera.enabled = false;
camera2.enabled = true;
setenemyTEXT2 ();
enemyTEXT2.enabled = true;
if (PKeyPressed)
{
THEREHealth2 -= 20;
}
if (THEREHealth2 == 0)
{
camera.enabled = false;
camera2.enabled = true;
Destroy (exampleTwo);
}
}
if (other.gameObject.name == "N3")
{
camera.enabled = false;
camera2.enabled = true;
setenemyTEXT3 ();
enemyTEXT3.enabled = true;
if (PKeyPressed)
{
THEREHealth3 -= 20;
}
if (THEREHealth3 == 0)
{
camera.enabled = false;
camera2.enabled = true;
Destroy (exampleThree);
}
}
if (other.gameObject.name == "N4")
{
camera.enabled = false;
camera2.enabled = true;
setenemyTEXT4 ();
enemyTEXT4.enabled = true;
if (PKeyPressed)
{
THEREHealth4 -= 20;
}
if (THEREHealth == 0)
{
camera.enabled = false;
camera2.enabled = true;
Destroy (exampleFour);
}
}
}
void setenemyTEXT ()
{
enemyTEXT.text = "life:" + THEREHealth.ToString ();
}
void setenemyTEXT2 ()
{
enemyTEXT2.text = "life:" + THEREHealth2.ToString ();
}
void setenemyTEXT3 ()
{
enemyTEXT3.text = "life:" + THEREHealth3.ToString ();
}
void setenemyTEXT4 ()
{
enemyTEXT4.text = "life:" + THEREHealth4.ToString ();
}
}
i did that but the... if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P)) { THEREHealth = THEREHealth - 20; } if (THEREHealth == 0) { camera.enabled = false; camera2.enabled = true;
Destroy (exampleOne); } part of it still won't work?
no nothing changed with that. its still not doing anything.
Have you verified that OnTriggerEnter() is entered? Also the code that I gave you is probably not working because P$$anonymous$$eyPressed is reset every frame. You can do the following ins$$anonymous$$d
In Update()
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.P)) // or Input.Get$$anonymous$$ey("P") or Input.GetButton("P")
{
P$$anonymous$$eyPressed = true;
}
This will set to true in the Update() function and will not be reset. Now in OnTriggerEnter() you just continue to check for P$$anonymous$$eyPressed but at the end of OnTriggerEnter() you reset P$$anonymous$$eyPressed to false. You may want to deter$$anonymous$$e if you want to reset P$$anonymous$$eyPressed somewhere else as well otherwise once you press the 'P' key P$$anonymous$$eyPressed will stay set until it is reset.
Your answer
![](https://koobas.hobune.stream/wayback/20220612075950im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
why OnTriggerEnter is not work if with void Update 2 Answers
Help to play music whenever the character enters a GameObject... 1 Answer
Setting OnTriggerEnter to a second object collider 2 Answers
OnTrigger Problems Between Colliders Issue 1 Answer
How to obtain a variable in update that doesn't behave like a loop? C# 1 Answer