- Home /
make a transparent object not transparent
hi, i have code that makes something transparent at the start of the game but i want it to be enabled when i right click here's my code to make it transparent:
void Start () { gameObject.GetComponent().enabled = false; }
here's my code I am trying to use to make it not transparent anymore:
void Update()
{
if (Input.GetMouseButtonDown(1))
{
gameObject.GetComponent<Renderer>().enabled = true;
}
}
}
it doesn't work and i don't know why, please help if you can
I dont remenber if GetComponent return disabled objects(i think it return), but try to use a field to hold the Renderer reference set it value in Start and use the reference in Update
GetComponent().enabled would not disable the game object.
Answer by Naphier · Oct 29, 2016 at 04:37 AM
We need more info, the code you show should work fine. enabled only disables components, not the entire gameObject. It will disable a script if that is the component you're Getting.
Did you deactivate your game object somewhere else, like in the inspector? If so then the scripts attached to it don't run.
This is invalid code:
void Start () { gameObject.GetComponent().enabled = false; }
GetComponent requires a parameter or a type.
Here's a simple example of working code:
public SpriteRenderer spriteRenderer;
void Start()
{
spriteRenderer.enabled = false;
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
spriteRenderer.enabled = true;
}
}
Answer by AurimasBlazulionis · Oct 28, 2016 at 08:53 PM
It will not work at all. In the start function you should call gameObject.GetComponent<Renderer>().enabled = false;
. Because now you disable the object and make it not detect your mouse input.
Your answer
Follow this Question
Related Questions
make an object slowly appear 2 Answers
Why is my object not moving? 1 Answer
How to clear only a selected baked object 0 Answers
How to make this image semi-transparent? 3 Answers
gameObject.SetActive(false); not working as intended 3 Answers