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
public Slider healthBarSlider;
public Text lowText;
public Text emptyText;
public GameObject weight;
public GameObject emptyWeight;
void Start(){
lowText.enabled = false;
emptyText.enabled = false;
weight = GameObject.Find("LowBoostWeight");
weight.SetActive(false);
emptyWeight = GameObject.Find("EmptyWeight");
emptyWeight.SetActive(false);
}
void Update(){
if (healthBarSlider.value < 10) {
lowText.enabled = true;
weight.SetActive (true);
}
if (healthBarSlider.value > 9) {
lowText.enabled = false;
weight.SetActive (false);
}
if (healthBarSlider.value < 2) {
emptyText.enabled = true;
lowText.enabled = false;
weight.SetActive (false);
}
if (healthBarSlider.value > 2) {
emptyText.enabled = false;
emptyWeight.SetActive (false);
}
if (healthBarSlider.value < 3) {
emptyWeight.SetActive (true);
}
if (Input.GetKey (KeyCode.LeftArrow)) {
healthBarSlider.value -= 0.015f;
}
}
void OnCollision2D (Collision2D collision){
if (collision.gameObject.tag == "CO") {
healthBarSlider.value += 9;
}
}
void OnTriggerEnter2D (Collider2D collider){
if (collider.gameObject.tag == "Bubble") {
healthBarSlider.value -= 9;
}
}
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.
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
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.
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 =)
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.
2268970–152034–Slider.zip (27.8 KB)
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 (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
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.
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?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SliderTscript : MonoBehaviour {
public Slider healthBarSlider;
// Use this for initialization
void Start () {
}
// Update is called once per frame
//void Update () {
// if (Input.GetKey (KeyCode.LeftArrow)) {
// healthBarSlider.value -= .001f;
// }
//}
void OnCollisionEnter2d(Collision2D other)
{
if (other.gameObject.tag == "SomeThing"){
healthBarSlider.value += .25f;
}
}
}
Have you tried a debug in the collision to see if it is still registering the collision?
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.
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.
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.
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.
Same end result again, when but it if statement within. There dots was red intstead of pink.
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
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.
No worries. Did the debug work once the typo was fixed? It should at least have entered the collision check this time.
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.
void OnCollisionEnter2D(Collision2D other)
{
Debug.Log ("Hey");
if (other.gameObject.tag == "SomeThing"){
healthBarSlider.value += 25f;
}
}
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.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GetHey : MonoBehaviour {
public Slider healthBarSlider;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Player"){
healthBarSlider.value += .25f;
}
}
}
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