Hide 3D object at start of scene until certain condition met - c#

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

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.

Toggle mesh renderer with a Unity event in Animator

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

Unity3D - Mesh Collider in imported blender mesh doesn't work

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

Unity text on cube prefab

I want to make a cube in Unity3D which can move and jump etc.... and I want to generate a random number (1-99). I want the number to be on the cube's every side. I don't want to make 99 texture I want to add the number with script. I read that I should add Text Mesh but i cant because I have Mesh Renderer.
Can somebody help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerThings : MonoBehaviour {
public static int player_name;
void Start () {
player_name = Random.Range (10, 99);
name = player_name.ToString();
}
// Update is called once per frame
void Update () {
}
}
If nothing is in your Update function, get rid of it. It's still being called every fraction of a second to check there is nothing in it. If this is persistent in your code a lot, it will eventually unnecessarily slow it down.
Next - A solution is to add an empty GameObject, whose children are the sides of your cube which require a Text Mesh.
Place each of the children in an array, and then do a foreach:
foreach(textarray as GameObject ta){
ta.getComponent<TextMesh>().text = name;
}

How to set position of gameobject for animation in script

I have two game object.
Both gameobject positions are different...
I attached same animation clip to both gameobject.
In one game object I am don't want any change in animation clip.
I just want to use same animation clip on different gameobject but animation timing is different
& In other Gameobject I am just want to play animation at 0.30 s but I not knowing how set position of this gameobject in script.
I am assign below script to second game object.
using UnityEngine;
using System.Collections;
public class ani3 : MonoBehaviour {
void Start () {
animation ["#cube"].time = 0.30f;
//how to set position of gameobjet
}
void Update () {
gameObject.animation.Play("#cube");
}
}
you can use a empty game object and nest one of the game object as child and change the position of the empty game object ,it is work for me.

Categories