Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Slider problem

Discussion in '2D' started by FlyingElf, Aug 20, 2015.

  1. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    Hi, i try to make those collider and collision make some changes for my slider. Actually it first work well, but some how it suddenly stop working. Maybe i made some stupid changes or something. Both of those gameobjects is tagged right and have 2d colliders. That with "Bubble"-tag is actually scripted also with some other commands(change player rigidbody rotation) and that work well. So i think problem is with slider.
    I also try number without (f) but no changes, also try with name instead of tag.
    Other script commands work well, only that Ontrigger and OnCollision one is silent.
    Sorry if this is stupid question, but im little lost with that one :eek:

    Code (csharp):
    1.  
    2.     public Slider healthBarSlider;
    3.     public Text lowText;
    4.     public Text emptyText;
    5.     public GameObject weight;
    6.     public GameObject emptyWeight;
    7.    
    8.     void Start(){
    9.         lowText.enabled = false;
    10.         emptyText.enabled = false;
    11.         weight = GameObject.Find("LowBoostWeight");
    12.         weight.SetActive(false);
    13.         emptyWeight = GameObject.Find("EmptyWeight");
    14.         emptyWeight.SetActive(false);
    15.  
    16.     }
    17.  
    18.     void Update(){
    19.  
    20.         if (healthBarSlider.value < 10) {
    21.             lowText.enabled = true;
    22.             weight.SetActive (true);
    23.         }
    24.         if (healthBarSlider.value > 9) {
    25.             lowText.enabled = false;
    26.             weight.SetActive (false);
    27.         }
    28.         if (healthBarSlider.value < 2) {
    29.             emptyText.enabled = true;
    30.             lowText.enabled = false;
    31.             weight.SetActive (false);
    32.         }
    33.         if (healthBarSlider.value > 2) {
    34.             emptyText.enabled = false;
    35.             emptyWeight.SetActive (false);
    36.         }
    37.         if (healthBarSlider.value < 3) {
    38.             emptyWeight.SetActive (true);
    39.         }
    40.         if (Input.GetKey (KeyCode.LeftArrow)) {
    41.             healthBarSlider.value -= 0.015f;
    42.         }
    43.     }
    44.  
    45.     void OnCollision2D (Collision2D collision){
    46.         if (collision.gameObject.tag == "CO") {
    47.             healthBarSlider.value += 9;
    48.         }
    49.     }
    50.  
    51.     void OnTriggerEnter2D (Collider2D collider){
    52.         if (collider.gameObject.tag == "Bubble") {
    53.             healthBarSlider.value -= 9;
    54.         }
    55.     }
    56.  
     
    Last edited: Aug 22, 2015
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    The bubble collider is set to trigger & the CO object collider isn't set to trigger? & at least one object in each collision/trigger has a rigid body?

    Also, please use code tags when posting code, it makes it easier to read. To clean it up a bit you could group some items in your if statements together.
     
  3. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    Thx, change tags to code and clean it little pit. Yeah i use that "bubble" with trigger option. I also make bubble disappear with trigger, after player go through it. It work well. There is rigidbody2D on every object (player, bubble, CO).
    Still can't figure this out, start getting gray hairs :D
     
  4. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    The script seems mostly alright if it is attached to a game object that has a 2D collider.

    Might want to change the function name on line 45 from OnCollision2D to OnCollisionEnter2D or one of its variants.
     
  5. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    There is polygon collider 2d on Player and CO, and edge2d on Bubble. I change that function name to OnCollisionEnter2D, but nothing happen. I try change script under slider also, instead on canvas where it usually been, but that not give change either. My slider set-up is direction left to right, min 0, max 20, no whole numbers.
    As i say, strange think is that it first work well. If i remember right after i but that line 40 (input getkey..) it stop working.
    Yeah i try also take that of, but it not give any goods.
    Well i also change those other value functions where i want them be, just don't get it how those can spoil that first one.
    Is there something i can do with debug on this one?
    btw. sorry about my poor english (finglish =)
     
    Last edited: Aug 25, 2015
  6. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    I have attached a project which has your script with just the OnColliderEnter2D and OnTriggerEnter2D functions that seems to work. Perhaps it can help you?

    You can move the center square with the left/right arrow keys and collide with or trigger the adjacent squares which would move the sliders.
     

    Attached Files:

  7. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    I don't know why, but i don't get that working at all. Nothing happening when use arrow keys.
    It say 'The referenced script on this Behaviour is missing!', is that test-script assigned, or should i but it some where?
    Probably i don't just know how to use that slider file :D (never tested anything like that before).
    Kind of question, can i change slider value via another script? Mean if but those OnCollider2d reference etc. to player script? I already try it little, but have some problems to get connection with slider.value from other script.
    Starting to feel problem really is some where else than script(but where..), cause it dont work gameObject.name either..
    Frustrating problem, why it work before :/
    - Ila
     
    Last edited: Aug 31, 2015
  8. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    And it's look's like collision and tagging work well for same objects on inside player script. Example that CO-tag get return when OnCollisionEnter2d(instead of smash the player, as some other objects).
    Can there be some what block collision/colliders working, inside slider script/options?
    And hey Chuan thx you try help me with that problem.
     
  9. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    Here i'm again. Still can't figure that mystery. I start new project just for testing that.
    Made only player (rigidbody2d,polygon2d), SomeThing to hit ( rigidbody2d, polygon2d) and slider.. And it's not work. I also try it without +/-, just give some straight command for value. Here is those scripts... that Input.GetKey still work great and change slider value. So collision or some tagging problem or what?
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class SliderTscript : MonoBehaviour {
    6.  
    7.     public Slider healthBarSlider;
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     //void Update () {
    17.     //    if (Input.GetKey (KeyCode.LeftArrow)) {
    18.     //        healthBarSlider.value -= .001f;
    19.     //    }
    20.     //}
    21.     void OnCollisionEnter2d(Collision2D other)
    22.     {
    23.         if (other.gameObject.tag == "SomeThing"){
    24.             healthBarSlider.value += .25f;
    25.         }
    26.     }
    27. }
     
  10. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Have you tried a debug in the collision to see if it is still registering the collision?
     
  11. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    Yep, i try. It's not say anything on the log(if i try it right..?). Actually, i can also write anythink on tag "name" and log is empty.
     
  12. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Put a break point on the collision check if statement. It sounds like the collision isn't being detected so next you to confirm that. The breakpoint will show you whether that is happening, if it is then you know there is something wrong with the code that follows, if it doesn't detect any collision at all the. You know you have a different issue.
     
  13. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    When i set the breakpoint to collision line 21, right? It give's that pink dot instead of red, also it's not totally freeze after play button only thinks little more after start. So collision isn't being detected?
    Sorry i'm little new on setting breakpoint etc. like a virgin ;)
    Is there way to but that slider value change, inside player script instead of slider script? When i try to but it there, there was some problem find slider and value etc. Meaning that collision looks working on player script.
     
  14. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Yeah, if you set the break point & assign it & nothing happens when you collide with something then the collision is t being detected.. Seems that that is where your issue is. To double check put the breakpoint on your if statement within the collision & check there.
     
  15. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    Same end result again, when but it if statement within. There dots was red intstead of pink.
     
  16. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Ok, it's definitely not getting into the collision then so now you need to look at why.

    Man, I just noticed a typo on your collision line. The D should be capitalised 2D
     
  17. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    I change it to capitalised 2D, it was only on this test project script. Actually i silent hope problem was some that silly thing, but no :( I try to figure something tomorrow, or trying to change it to insider another gameobject script. Hopefully i can move along soon :/ Thx, for trying to help me Ted.
     
  18. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    No worries. Did the debug work once the typo was fixed? It should at least have entered the collision check this time.
     
  19. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    Console not say anything after try debug with that fixed typo. I do it like this, and also try put debug.log inside if() etc.
    Code (csharp):
    1.  
    2.     void OnCollisionEnter2D(Collision2D other)
    3.     {
    4.         Debug.Log ("Hey");
    5.         if (other.gameObject.tag == "SomeThing"){
    6.             healthBarSlider.value += 25f;
    7.         }
    8.     }
    9.  
     
  20. FlyingElf

    FlyingElf

    Joined:
    Aug 2, 2015
    Posts:
    12
    Hey, after i put another script to the inside gameobject instead slider, it start to work at least some way. Now it change slider value and get collision2d. As note i also change tag as "Player" with of course too help. Still i don't figure why it's not detect any collision inside slider script.
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class GetHey : MonoBehaviour {
    6.  
    7.     public Slider healthBarSlider;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.    
    17.     }
    18.     void OnCollisionEnter2D(Collision2D other)
    19.     {
    20.         if (other.gameObject.tag == "Player"){
    21.             healthBarSlider.value += .25f;
    22.         }
    23.     }
    24. }
    One new thing for me more is that, GetHey script not even has to be active, and it still a work. There isn't anymore be any collision2d reference inside old slider script.
    Probably i need to do some timing to that value change, cause it's instant now. But now i feel like HERO, fight almost three weeks with that tiny problem :D
     
  21. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Well done, it does feel good when you finally make a breakthrough no matter how small it is. Keep working through it slowly & you will eventually get there.
    Debug logs are useful as you can work backwards through your logic to identify where it works & where it stops working so don't be afraid to use them & break points. This can help you find the area or data that you need to focus on (sometimes it isn't where the debug shows the break, it can be somewhere else that has failed to correctly pass info across to that point).
     
    FlyingElf likes this.