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 Sek19 · Nov 20, 2015 at 11:31 AM · triggerif-statementswhilenestedreturn value

script not returning values in if statements nested in for loop

Hello, I am having trouble with this script. First off you will notice I have a few things referenced in this that are not being used at the moment pleases ignore these for the time being.

What I am trying to do is, take a building that is set to its shader color and change its color to the tank that enters its overlapShere. After it has been "capturing" for 30f. For some reason when my if statements are nested in the for loop I am not returning any values to add up to the 30f. When moved out of the for loop they return the values but I have trouble referencing when the tank ridgid body is in the overlapSphere. this is probably the biggest nube question of the year but any help would be greatly appreciated.

P.S. since the code is sitting in private void OnTriggerEnter(Collider other) I have tried placing the if statements in a void OnTriggerStay(Collider other), my understanding is that if OnTriggerEnter detects a ridgid body the OnTriggerStay should be activated in code and play out the code inside. I was returned with the same issue though (if statements not returning any values +/- for m_capturing) . So any feedback on what i'm doing wrong with that, or if this is the wrong approach would be great to know!

Thank you!

     using UnityEngine;
     using UnityEngine.UI;
     
     using System.Collections;
     
     public class BuildingCapture : MonoBehaviour {
     
         public Object Building;                                // Building Object
         public Color ColorStart = Color.gray;                  // keep building at grey
         public Color ColorEnd = Color.red;                     // Set building color to red once tank is found
         public Renderer rend;                                  
     
         public float m_CaptureRadius = 30f;                     // distance of capture area
         public float m_CaptureTime = 20f;                       // capturing time needed before captured
         public float m_Capturing = 0f;                          // Active Capturing time
         public Slider m_CaptureSlider;                          // slider indicating capture owner
         private float m_CurrentCapturing;
     
         public LayerMask m_TankMask;
         public float duration = .5f;
         public Color m_PlayerColor = Color.red;
         [HideInInspector] public int m_PlayerNumber;            // player owner of building
         [HideInInspector] public GameObject m_Instance;  // 
         public Vector3[] m_tankColorV;
         public Vector2[] newt;
         bool capturing = false;
     
         public Shader shader;
     
         void Start()
         {
             rend = GetComponent<Renderer>();
         }
         
     
         private void OnEnable()
         {
             m_CurrentCapturing = m_Capturing;
         }
     
     
     
         private void OnTriggerEnter(Collider other)
         {
             
     
             // Creates the Collider Sphere 
             Collider[] colliders = Physics.OverlapSphere(transform.position, m_CaptureRadius, m_TankMask);
     
             // Checks entire range of colliders array (Sphere)
             for (int i = 0; i < colliders.Length; i++)
             {
     
                 // find their rigidbody
                 Rigidbody targetRigidbody = colliders[i].GetComponent<Rigidbody>();
     
                 if (!targetRigidbody)  // if targetRigidbody not there
                     continue;
     
                 capturing = true;
     
                 if (capturing == true)
                 {
                     while (m_Capturing <= 30)
                     {
                         m_Capturing += 1f;
                     }
     
                 }
                 else if (capturing == false)
                 {
                     while (m_Capturing >= 1)
                     {
                         m_Capturing = 0f;
                     }
                 }
                 else if (m_Capturing == 30f)
                 {
                     Renderer rend = GetComponent<Renderer>();
                     rend.material.shader = Shader.Find("Standard");
                     rend.material.SetColor("_Color", Color.red);
                 }
     
                 //m_Capturing += 1f;
             }
     
 
                     
                     }
 
Comment
Add comment
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

1 Reply

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

Answer by saschandroid · Nov 20, 2015 at 12:18 PM

In line 60 you are setting capturing = true; so the first if(capturing==true) (line 62) is ALWAYS entered and both else if can never be reached.

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 Bonfire-Boy · Nov 20, 2015 at 12:26 PM 0
Share

Also worth mentioning that even with line 60 removed, the third clause will never be reached. The first 2 clauses ( if (capturing == true) and else if (capturing == false)) encompass all possibilities.

avatar image Sek19 · Nov 20, 2015 at 01:57 PM 0
Share

So shouldn't it start incrementing? Since the code is true it should run through at least the m_Capturing += 1f; When in the for loop it returns no value to m_Capturing. i added capturing = true; to be sure it was hitting that case for testing purposes. But was unaware of the logic loop being incorrect I'll be fixing this. ' capturing = true;

           if (capturing == true)
           {
               while (m_Capturing <= 30)
               {
                   m_Capturing += 1f;
               } '
avatar image Bonfire-Boy Sek19 · Nov 20, 2015 at 02:09 PM 0
Share

Yes it will start incrementing. As it is, so long as there's an overlapping collider with a rigidbody, it will always increment (until m_Capturing == 30, after which it will do nothing).

I would recommend adding some logging so that you know where it's getting to in the function and what the important values are (for example, how do you know that there are any overlapping colliders with rigidbodies?).

And fixing the logic, obviously.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using function return in if statement 2 Answers

How do I check for mouseclicks inside a nested if statement. 2 Answers

Detecting if Player is In Range of Items? 1 Answer

Color triggered collider? 0 Answers

How to cancel a if statement once touching a collider 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