Unity Community

Results 1 to 7 of 7

  1. Location

    OnGUI() problem - firing twice a time! [SOLVED]

    Hello guys!
    First of all, excuse me for my english; i'm a spanish native speaker.

    I'm learning how to develop games in Unity 3D by myself, so i decided to make a FPS to play with my friends (the best motivation i found to achieve my goal).
    I wrote my weapon's manager (SC_ManagerArmas.cs) which listen through OnGUI method (it doesn't listen really... well, i think you may understand what i'm trying to say) what kind of key is pressed by the gamer, and then changes the character weapon: 1 for granade launcher, 2 for shuriken launcher, and 3 for a pistol.

    The problem - firing weapons:
    If i put the code that allow firing the current weapon into Update() method, everything go well; however, if i put the code into OnGUI() method, the weapon shots twices a time.

    Anyone can help me with this issue?

    Thaks a lot!

    ps: you will see an interface's method call; i like more implement an interface rather than use a broadcasting.

    1. using UnityEngine;
    2. using System.Collections;
    4. public class SC_ManagerArmas : MonoBehaviour {
    6.    private Camera vision;
    7.    private RaycastHit rayResultado;
    8.    private Ray rayoVision;
    9.    private IArmas armaActual;
    11.    //i'm setting the vision for my raycast and first weapon
    12.    private void Awake () {
    13.       vision = gameObject.transform.parent.camera;
    14.       elegirArma(0);
    15.    }
    17.    //firing a weapon!... here it shots once a time
    18.    private void Update() {
    19.       if (Input.GetButtonDown("Fire1")) {
    20.       rayoVision = vision.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
    22.       if (!Physics.Raycast(rayoVision, out rayResultado))rayResultado.point = Vector3.zero;
    24.       armaActual.Disparar(rayResultado);
    25.       }
    26.     }
    28.    private void OnGUI() {
    29.         if (Input.GetKeyDown("1")) {
    30.             elegirArma(0);
    31.         } else if (Input.GetKeyDown("2")) {
    32.             elegirArma(1);
    33.         } else if (Input.GetKeyDown("3")) {
    34.             elegirArma(2);
    35.     }
    36. /* SHOTS TWICE!
    37.         else if (Input.GetButtonDown("Fire1")) {
    38.            rayoVision = vision.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
    39.            if (!Physics.Raycast(rayoVision, out rayResultado))rayResultado.point = Vector3.zero;
    40.             armaActual.Disparar(rayResultado);
    41.     }*/
    42.     }
    44.    private void elegirArma(int indice) {
    45.       GameObject goTemp;
    46.       for (int i=0; i< gameObject.transform.childCount; i++) {     
    47.          goTemp = transform.GetChild(i).gameObject;
    48.          if (i == indice) {
    49.             goTemp.SetActiveRecursively(true);
    50.             armaActual = (IArmas) goTemp.GetComponentInChildren(typeof(IArmas));
    51.           } else goTemp.SetActiveRecursively(false);
    53.       }
    55.    }
    57. }

  2. Location
    Lognes, France
    Why would you set irrelevant code in the OnGUI function instead of the Update ?

    Who told you to put this code in OnGUI, if Update works ?
    A lazy big fat chicken hibernating, sleeping peacefully.

  3. Volunteer Moderator

    Re: OnGUI() problem - firing twice a time!

    OnGUI runs at least twice per frame, by design. Don't use it for anything except GUI code.

    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes

  4. Location
    AkilaeTribe, Eric5h5, thanks for your answers.

    As you said Eric5h5, i just realize OnGUI it's called several times per frame; i have to recognize that i could check this out at Unity Script Reference before post my doubt here... a friend of mine told me that there wasn't any explanation in the web. Next time, i will take a look.

    AkilaeTribe, i wrote the code OnGUI because i thought was the right place where to put it; i'm a newbie and i'm giving my best, but looks like i have a long road to go... sorry

    Perhaps this post will serve to someone who, like me, make this mistake.

    Thanks people!

  5. Location
    Don't be so hard on yourself Kokumo, you actually did the right thing.
    Asking questions to find out that you did it the wrong way.
    That is how we learn things!

    All the best, Robert
    Available for 3D related stuff.
    Checkout my website: www.visualconduct.com
    Game-Content: www.visualconduct.com/shop

  6. Posts
    I did some test about this topic. And I have some conclusions. If there is anything wrong , please tell me.

    First, we know that unity will call Update() function per frame, and will call OnGUI() per Event.
    When one frame is updated , Unity will receive more than one Event, and need to initiate that event before call OnGUI. and the initiate will also send one event to OnGUI, that is EventType.Layout.
    Except EventType.Layout, Unity will send EventType.Layout before all Events are send to OnGUI.
    So we know every frame updated, OnGUI will called at least two times. The first one is EventType.Layout , the second one isEventType.Repaint.

    Before receiving EventType.Repaint,OnGUI will be called by all other Events. ex: EventType.MouseMove, after that, called by EventType.Repaint.
    Of course, before EventType.MouseMove and EventType.Repaint , there are also EventType.Layout.

    So you can do the test, if you move the mouse , than, your OnGUI will be called my four Events during one frame is updated. Those four events are:

    Here is my test code:
    1. function Update () {
    2.    print("Update Event.current = "+Event.current + "     Time.time " + Time.time + "     Time.realtimeSinceStartup " + Time.realtimeSinceStartup);
    3. }
    4. function OnGUI ()
    5. {
    6.    print("OnGUI Event.current = "+Event.current + "     Time.time "+ Time.time + "     Time.realtimeSinceStartup " + Time.realtimeSinceStartup);
    7. }

    It refer by those data below:

    Here are some import hints:
    1.For each event OnGUI is called in the scripts;
    EventType.Layout is sent prior to anything else - this is a chance to perform any initialization. It is used by the automatic layout system.
    All other events are processed first, then the repaint event is sent.
    4.Event Execution Order

  7. Posts
    There is another topic, I want to discuss.

    if I use function Update() to receive the iPhoneTouch event

    we have example in the document just like this:
    1. function Update () {
    2.     if (iPhoneInput.touchCount > 0 )
    3.     {
    4.         if(iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Ended)
    5.         {
    6.             //...
    7.         }
    8.     }
    9. }

    But I notice there is a special situation happened.
    that is iPhoneTouchPhase.Ended Event happened but iPhoneInput.touchCount = 0 in the function Update.
    Is there anyone have this problem?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts