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.
Related
Developing a 3D VR application on Unity using OpenXR (2021.3.11f1).
I'm trying to make it so that a Canvas is hidden until a certain condition is met. That condition is that another 3D object's x position is under 45. Here is my script right now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OptionsBox : MonoBehaviour
{
public GameObject canvas;
public GameObject playerObj;
void Start()
{
canvas.SetActive(false);
}
void Update()
{
if (playerObj.transform.position.x < 45){
canvas.SetActive(true);
}
}
}
I then made an empty GameObject and inputted the script into there:
However, when I run my scene, the canvas is still displayed. What have I done wrong?
No problem with the script. Problem was the other object started at a x value less than 45, so it was always set to True.
You can add a CanvasGroup to the Canvas and play with the opacity property called Alpha
Hi guys I'm pretty new to unity and I am trying to make an animation where some of the meshes within a prefab only appear ~halfway through the animation.
I have added an event at the desired time within animator but I cant figure out exactly what code I need to execute to turn the mesh renderer on.
So far I have this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class toggleMesh : MonoBehaviour
{
public Renderer rend;
// Start is called before the first frame update
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = false;
}
// Update is called once per frame
public void toggleMeshR()
{
rend.enabled = true;
}
}
Any help with this would be appreciated
If you have an animation as you said, put an animation event on the frame(time) you desire to call your toggleMeshR().
When you right click on timeline of animation, you can create animation events.
More information: https://docs.unity3d.com/Manual/script-AnimationWindowEvent.html
Well, I'm trying to make an fps where you shoot tagets and appears an "Arcade-Style" score over them on unity 5, but, I don´t really know how to do it, already tried with an OnCollisionEnter(), but I did something wrong and it didn't worked, What can I do? Below you can see my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Diana : MonoBehaviour {
public GameObject diana;
public AnimationClip Score;
private Animation myAnimation;
/* IEnumerator Wait()
{
myAnimation = GetComponent<Animation>();
myAnimation.Play("DianaScore");
yield return new WaitForSeconds(3);
} */
void OnTriggerEnter(Collider other)
{
Debug.Log("Funcó wacho");
myAnimation = GetComponent<Animation>();
myAnimation.Play("DianaScore");
// StartCoroutine(Wait());
}
void Update () {
}
}
(Sorry for my bad english, I´m argentinian and new in all this coding thing)
I suggest putting this line of code: myAnimation = GetComponent(); into a Awake() function. If the above method does not work, I suggest checking that the Is Trigger is checked for OnTriggerEnter. If the console message (Debug.Log) shows, the problem might be something to do to the animation not the script. Last but not least, check that the bullet actually collides. I know this might sound crazy, but sometimes the bullet might be able to "not get detected" if it has enough velocity. Hope these suggestions work :D .
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.
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){}