How to add / update an integer value only once?

Hey guys,

The idea is to add one to an integer based on the rotation of the camera.

The whole code works except the value is incremented continuously in an update style format instead of just once? Even though putting the code in separate functions (not update).

I’ve tried putting the if statements in a custom function but then nothing works.

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataCamera : MonoBehaviour {

    public GameObject camera;
    public float a;

    public int left = 0;
    public int right = 0;
    public int top = 0;
    public int bottom = 0;

    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {

        a = camera.transform.rotation.eulerAngles.y;
        print (camera.transform.rotation.eulerAngles.y);


        if (a < 90)
        {
          
            LeftFunction ();

        }

        if (a > 90 && a < 180)
        {
            RightFunction ();

        }


        if (a > 180 && a < 240)
        {
            TopFunction ();

        }


        if (a > 240 )
        {
            BottomFunction ();

        }
}



    void LeftFunction()
    {
       
        left ++;
        Debug.Log("less than 90" + left);
    }

    void RightFunction()
    {
        right++;
        Debug.Log("more than 90" + right);
        return;
    }

    void TopFunction()
    {
        top++;
        Debug.Log("more than 90" + top);
        return;
    }

    void BottomFunction()
    {
        bottom++;
        Debug.Log("more than 240" + bottom);
        return;
    }

}

So basically if the camera rotation is below 90, I only want the left variable to be added once so 0 + 1 = 1 and then to go back to the main update function (I even tried using return)

Instead of 0 + 0 = 1 and then returning, the variable is continuously updated.

Well, can the values ever be back to zero ? I don’t see that happening at all, but maybe I don’t understand your larger plan.
One idea would be to simply set everything to zero at the start of Update(), then adjust whichever to 1… then presumably use them somewhere?
Then you could improve that further…

However, what is your end goal? You’re using these values for something else, right?
Is it something that you could calculate (your 1s and 0s) when you need it, or do you always need it?
Can you adjust it only when the camera moves (for either scenario)?
:slight_smile:

1 Like

Yes. We need a lot more “what I really want to accomplish is …” and a lot less “tell me how to make this implementation decision I’ve already selected fit my problem that you don’t know” on this forum in general.

Do you only want this to happen once at the Start() perhaps or perhaps like on like a trigger being entered OnTriggerEnter()? If you set something to happen every frame it will happen every frame

The idea is to split the scene into 4 sections based on direction. Front, left, right and back.

When the camera faces one direction I want the integer of the certain relevant variable to increment by one

so left++ (left being set as 0 during start) should now be 1. I want to add 1 only once instead of updating it.

During play in the editor if I rotate the camera to bottom or back then the bottom variable changes to 1 from 0, if I move back to forward the forward variable which is 1, changes to 2 now.

I can see the code work during play however instead of adding just 1 it adds for infinity until I change the direction I want.

So basically this has something to do with the update function as update applies something every frame but if I put the code in any other function it doesn’t work.

Also in the Update function, I don’t call the actual code to add, I call the function so if a condition is true then open that custom function and then ++ but even then it’s adding infinitely.

So basically how do I add and store into a variable once instead of continuously updating the variable?

Here is a screenshot from my program, as you can see the code work but instead of seeing 1, 2, 3, I see double and triple digit numbers as they are continuously updating based on position.

So it’s adding, it’s working, but it;s adding forever instead of just once

The idea is to understand which parts of the 360 video in VR are watched. This is for user analysis.

3277005--253391--code.png

I just want to add a value to a variable once, in this case 1, instead of continuously adding 1

So if the camera rotation is within a certain value like 90 or under then a = a + 1 which should be 1 now, but instead it just keeps adding and adding

I know this has something to do with the Update function but the actual + codes are in separate custom functions called in the update functions but I guess the 2 are linked because I am saying if a < 90 in the update function then do something

Maybe instead of the update I should put all of this in the start function? But if I do that the program starts at 1 then never updates ever again even if I rotate the camera to back position and then back to forward. So the code works in update or the way I posted it

For further information check my reply above, i included a picture as well

The idea is to see which parts of the scene and in the future the 360 video are focused on by the user.

Hmm I guess a trigger should work right?

As mentioned above, I want to divide the scene based on direction so left, right, bottom and top

I want to add one to the direction the camera is facing

so if the camera is below 90 degrees and is facing forward I want to add one to forward and if I move to the right add one to right and if I move back to forward add 1 again so now that would be 2 (1 + 1)

You could check to see if the rotation changed. That would cut it down, but if it was rotating slowly it would still give many calls to the function. The other thing you could do would be to check if they were the same for a second or something, then call the function and flag it so it didn’t happen again until there was another rotation change.

thisRotation = camera.transform.rotation...
if(thisRotation != lastRotation)
  cameraMoved = true;
lastRotation = thisRotation;

if(cameraMoved && thisRotation < 90)
   LeftFunction();

So, from what you’ve written, I’d say you could continue using ints or use bools.
Then, I’d say just update it on movement, or put it in a coroutine to slow down the number of updates.
I’d say zero them all during whichever “loop”, and then reassign 1 or true if applicable.
I think that will do what you want.