I'm trying to apply a script that changes the material color when the cursor is on top of the object. Here's the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColor : MonoBehaviour {
public Color startColor;
public Color mouseOverColor;
bool mouseOver = false;
void OnMouseEnter(){
Debug.Log("START");
mouseOver = true;
GetComponent<Renderer>().material.SetColor("_Color",mouseOverColor);
Debug.Log("TESTE");
}
void OnMouseExit(){
mouseOver = false;
GetComponent<Renderer>().material.SetColor("_Color", startColor);
}
}
The object is a simple triangle:
The script only works when the object has a Sphere Collider (what i'm looking for is to use a Mesh Collider).
Can someone help me understand how to use it with a Mesh Collider?
Thank you
According to the Unity's documentation, OnMouseEnter() function only works when your Collider is marked as trigger.
Try activating that property and check the documentation too.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseEnter.html
Related
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.
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
I have a texture issue with a VR whiteboard. When I attach a texture to a plane in Unity that has the whiteboard.cs script attached, the whiteboard plane defaults to plain white when I press run. The plane is still reactive to the marker in a VR space, but I want to be able to put up a stencil texture/material that allows users to trace it. I've seen the similar issue when the variables are private and not when set to public. Has anyone experienced the same texture GetComponent issue? I'm using Unity 2021.2,10f.
Attached is a screenshot of the object before and after pressing run and the mentioned script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Whiteboard : MonoBehaviour
{
public Texture2D texture;
public Vector2 textureSize = new Vector2(x:2048, y:2048);
void Start()
{
var r = GetComponent<Renderer>();
texture = new Texture2D(width:(int)textureSize.x,
height:(int)textureSize.y);
r.material.mainTexture = texture;
}
}
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
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.