• 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 /
avatar image
Question by Gruffy · Oct 30, 2013 at 08:06 PM · mathconditional

stuck with a loop/algorithm

Hey All, its been a while... Hope everyone is well on here.

My question is simply this, lol..

I created a little method to run fired through OnTriggerExit() to check for an equal division and if so instantiate a prefab. My current issue lies in my brain not functioning correctly and as a result I cant seem to work out how apply it in what`s happening below (which is correctly doing the job at evens) but whilst checking to see if the current value held in honeyCreated is not the same as it was the last time the method was fired.

in steps: on method run.... 1.check if honeyCreated is not at 0 1. if not at 0, then make sure throughput method only produces prefab at every even increment 3. only instantiate if the number is even but not if its the same number found at the last check. whats happening? Every time this runs its fine, until i get past the honeyCreated value storing 2. My factory on fills only every two deposits, but the trigger is there all the time. I have a funny feeling my stupidity will unfold in a majestic way once you amazing dudes get your heads on it.

Thanks for reading...method where the initial conditionals are happening below..:)

     void CheckHiveThroughput(int honeyCreated)
     {
         
         if(honeyCreated != 0)
         {
             if(honeyCreated%2 !=0)
             {
                 //moveHoney = false;
                 return;
             }
             else
             {
                 mPrefab = (GameObject)Instantiate(prefab, posToInstantiate, Quaternion.identity);
                 //moveHoney = true;
                 //mPrefab.transform.position = Vector3.Lerp (posToInstantiate, mMoveObjTo, timeStamp);    
             }
         }    
     }
 

EDIT: Here is whole script to clarify where things are coming from and how it updates the honeyCreated value outside the method im on about , by accessing a static value to update. (just testing with statics, will encapsulate later once working) :0 Thanks again, for reading this. gruffy

         using UnityEngine;
         using System.Collections;
         //gruffy 2013
         //used in conjunction with fillbar.cs , this script removes the amount collected in one bar and fill another in its place
         
         public class EmptybarTrigger : MonoBehaviour 
         {
         //inspector available public caches 
             public float speed = 0.5f;
             public Vector3 posToInstantiate;
             public Vector3 moveObjTo;
             public GameObject prefab;
         //private caches for above 
             private Vector3 mMoveObjTo;
             private GameObject mPrefab;
             private float timeStamp = 0.0f;
         //before the script is called
             void Awake()
             {
                 mPrefab = prefab;
                 mMoveObjTo = moveObjTo;
             }
         
         //collision based on time inside the collider
             void OnTriggerStay() 
             {
                 if(!FillbarTrigger.hasPollen) //if theres no pollen in the fill bar for pollen
                 {
                     //do nothing
                     return;
                 }
                 else //there is pollen in the fillbar
                 {    
                     Fillbar.NectarDisplay -= speed * Time.deltaTime; //reduce amount from the pollen fillbar display
                     
                     if (Fillbar.NectarDisplay <= 0.0f) 
                     {
                         Fillbar.NectarDisplay = 0.0f;
                         
                     } 
                     
                     for (float i = 0.0f; i < Fillbar.NectarDisplay; i++)
                     {
                         Fillbar.HoneyDisplay += (speed/2) * Time.deltaTime;
                         
                         if (Fillbar.HoneyDisplay >= 1.0f) 
                         {
                             //return to zero 
                             Fillbar.HoneyDisplay = 1.0f;
                             
                         }
                     }
                 }
         
             }
         //action after exiting object`s trigger collider 
             void OnTriggerExit()
             {
                 if(Fillbar.HoneyDisplay >= 0.99f)//0.99f because you can never be sure to reach an exact 1.0 value with floats 
                                                  //(could be replaced with a double precision but would require typecasting furthre donw the chain)
                 {
                     
                     Fillbar.HivesComplete++;  //add one to hives complete
                     
                     Fillbar.HoneyDisplay = 0.0f;  //return honey display to 0
                 }
                 CheckHiveThroughput(Fillbar.HivesComplete);
                 
             }
         //checks for hive output
         //if enough nectar delivered, hive quota filled at least twice(until we flesh out the switch later) we cretae a prefab applied at the inspector
             void CheckHiveThroughput(int honeyCreated)
             {
                 int curCreated;
                 //gruffy , you must change the switch
                 if(honeyCreated != 0)
                 {
                     if(honeyCreated%2 !=0)
                     {
                         //moveHoney = false;
                         return;
                     }
                     else
                     {
                         mPrefab = (GameObject)Instantiate(prefab, posToInstantiate, Quaternion.identity);
                         //moveHoney = true;
                         //mPrefab.transform.position = Vector3.Lerp (posToInstantiate, mMoveObjTo, timeStamp);    
                     }
                 }    
             }
                     
         }
Comment

People who like this

0 Show 2
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 Dracorat · Oct 30, 2013 at 08:12 PM 0
Share

I don't see a honeyCreated += 1 anywhere??

avatar image Gruffy · Oct 30, 2013 at 11:44 PM 0
Share

Hi Dracorat, I have edited my post to reflect the whole script to show where things are coming form Hope that sheds some light etc :) Gruffy

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

16 People are following this question.

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

Related Questions

Shear transformation using GameObject transformations 2 Answers

Why say Unity that 320* (1 / 320) = 0? 5 Answers

Measuring distance on Camera Screen-Space Canvas in -canvas units- 1 Answer

Are there any easing functions built into Unity 5? 1 Answer

Calculating curves based on gravity and speed then drawing line along that path 0 Answers


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