Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by willleo10 · May 01, 2016 at 06:35 PM · c#updateontriggerenter

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 ();

     }

     
         

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image willleo10 · May 01, 2016 at 06:28 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TBruce · May 01, 2016 at 06:53 PM

@willleo10

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 ();
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image willleo10 · May 01, 2016 at 07:03 PM 0
Share

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?

avatar image willleo10 · May 01, 2016 at 08:00 PM 0
Share

no nothing changed with that. its still not doing anything.

avatar image TBruce willleo10 · May 01, 2016 at 08:42 PM 0
Share

@willleo10

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges