Button switching color in unity - c#

I am very new to unity and coding in general, I am trying to create a button in unity that eventually opens a door in my game, but I immediatle ran into a problem, when I collide with the button it doesnt switch color, Im not sure if something is wrong with my code or my settings in unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Button : MonoBehaviour
{
[SerializeField]
GameObject switchOn;
[SerializeField]
GameObject switchOff;
public bool isOn = false;
void Start ()
{
gameObject.GetComponent<SpriteRenderer>().sprite = switchOff.GetComponent<SpriteRenderer>().sprite;
}
void OnTriggerEnter2D(Collider2D col)
{
gameObject.GetComponent<SpriteRenderer>().sprite = switchOn.GetComponent<SpriteRenderer>().sprite;
isOn = true;
}
}
I have 2 different button textures put on the on and off gameObjects a green one and a red one, does anyone know if something is wrong with the code

debug a line in your on collision enter, likely its not getting called. Make sure your player has a rigidbody and a collider and make sure your door has a collider. Also make sure trigger is checked on your player collider.

Related

How to change a sprite with a script Unity

I'm new to Unity and C#. I'm trying to make the sprite of "bird" change when he dies in unity. I tried following some tutorials but it doesn't work, so now I'm just trying to make the sprite change when "A" is pressed, but it still doesn't work. Is it a problem of the script or of the sprite?
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeSprite : MonoBehaviour
{
public Sprite deadBird;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
GetComponent<SpriteRenderer>().sprite = deadBird;
}
}
}
Here is a screenshot: https://i.stack.imgur.com/PTd9W.png
I think #derHugo is correct, if you have an Animator then that will most likely overwrite any change you try and do. To fix this, you can use that animator and create an animation that is the deadBird. once you have that, connect it to the animator controller from the normal state. in that connection you can create and set a new animator boolean like "isDead" and set it to switch to the dead animation is the bool is true. then change your code from
GetComponent<SpriteRenderer>().sprite = deadBird;
To
anim = gameObject.GetComponent<Animator>() //place this instead of the bird sprite
anim.SetBool("isDead", true); //place this in the if statement
Hope that helps! it's much cleaner to go through the animator as it allows for easier changes as you build your game.

How can I make the animation of a button happen when pressed on the mobile screen?

I'm sorry if my English is bad at first.
The button
This is the button I want to run the animation when pressed.
Animations
Sprites I want to change between these two states when pressing the button
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
public class Button : MonoBehaviour
{
private Animator animator;
public void SetTrigger(string Pressed) { }
Collider2D col;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
animator.SetFloat("Position", 0);
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
if (touch.phase == TouchPhase.Began)
{
Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
if (col == touchedCollider)
{
animator.SetFloat("Position", 1);
}
}
}
}
}
This is how the code has been after the last attempt I have made, it has things that may not make sense because I have been mixing things while testing. I have tried it with floats, bools and triggers. With the bool it has worked halfway for me, if I touched the button it did not sink but if from unity I pressed the button manually changing the boolean to true, and after touching from the mobile screen it recognized the touch and returned the button to the original position. In all situations what did not work well was the touch control but I have revised the touch control code and I would say that it is fine.
Sorry if it is not understood well, I am new to unity and programming, also English is not my main language
Are you sure Button does not already have implementation for what you are trying to achieve? Check what options are available under "Transition" in Button inspector. You can pick between Color Tint, Sprite Swap or Animation there, I suppose it handles most of the cases and I've never seen anyone implemeting animations to button in custom way.

Can the FireComplex particle be activated on trigger only?

I am using the FireComplex particle to generate an explosion (a bomb) when the user presses spacebar. The fire is playing on awake which I don't want and it doesn't seem to include an option to disable it.
Can this be coded to only activate the fire particle when I press space? I have tried the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CombatController : MonoBehaviour {
//public GameObject enemy;
[SerializeField]
public GameObject Bomb;
public GameObject Fire;
if(Input.GetKeyUp(KeyCode.Space)) {
BombExplosion ();
}
public void BombExplosion () {
//Create the Bombs Explosion Particle Effect.
Instantiate (Fire, this.gameObject.transform.position, Quaternion.identity);
Fire.Emit(5); //Burn for 5 seconds
}
Unity seems to be ignoring the Fire.Emit function.
The reason, I think, why the Bomb goes off after executing the game is because of the onviously incorrect logic in GetKeyUp. I doubt that it what you need.
Change it to GetKey instead.
Hope that clarifies the issue.

Unity 4 Adding GameObject and Collider References

How can I add the game object from above (Roed Knap, Hand etc.) into the script given below (at the picture)?
This is an example project. I can't figure out to reference GameObject's and Colliders into a Script.
What I want to do is very simple.
Make a GameObject with a Collider, and trigger something when collision happens.
So What I have is basically a GameObject Cube that has a Collider sphere added and for this isTrigger is selected. I want this trigger when entering, modify the text. Can you help me with initializing, referencing and other stuffs necessary, see below code. This is the code I work with.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
GameObject Cube;
GUIText Text;
Collider collision;
// Use this for initialization
void Start () {
collision = Cube.GetComponent<Collider> ();
Text = GetComponent ("GuiText") as GUIText;
}
// Update is called once per frame
void Update () {
}
void onTriggerEnter(){
Text.text = "Won"
}
}
Unity uses C# syntax for all of MonoBehaviour and engine namings. That is, a method starts with cap letter:
void OnTriggerEnter(Collider col){}

How to Selectively Disable Sphere Colliders?

I'm trying to build a pool game where the pot has a collider. Now on collision with a ball, I expect that particular ball that collided to disable.
The code below is what I've tried but it only allows me to set the ball manually. How do I automat and detect the right ball directly?
using UnityEngine;
using System.Collections;
public class pot: MonoBehaviour
{ //allows me to set collider
public SphereCollider ball;
void OnTriggerEnter(Collider other) {
Debug.Log("Ball potted");
ball = GetComponent<SphereCollider>();
ball.enabled = !ball.enabled;
}
}
Is
other.enabled = false;
what you are looking for?
It looks like your code is getting the sphere collider of the pot itself. Isn't "other" the ball in this case?
You can disable the "other" object (assuming it is the ball) with "other.gameObject.SetActive(false)" -- that's if you want the whole ball to disappear. If you just want it's collider to stop working then use "other.enabled = false".
public SphereCollider sphereCollider;
void Update()
{
if (Input.GetKeyDown(KeyCode.Q)) //or however you want to call it.
{
sphereCollider = gameObject.GetComponent<SphereCollider>();
sphereCollider.enabled = false;
}
}

Categories