- Home /
Enable SpriteRenderer issue
Hi there this could be something simple but the issue I’m having is I’m disabling a sprite renderer in the beginning & I’m trying to enable it for the rest of the game when I press a button but for some reason it is not enabling I’ve tried everything I learned on my own but it’s not working I’m new to unity
void Start ()
{
Ball.GetComponent<SpriteRenderer>().enabled = false;
}
void Update ()
{
isPaused = true;
Time.timeScale = 0;
}
public void Play ()
{
StartMenu.SetActive(false);
Time.timeScale = 1;
isPaused = false;
Ball.GetComponent<SpriteRenderer>().enabled = true;
}
Hey @Cookiemonster04 - Update()
is being called for every frame. Which means when you run your function Play()
, it will set isPause=true
and Time.timeScale=1
again in the next frame. Do you save a reference to the ball in a variable you haven't included in the code example?
Answer by niiicolai · Aug 09, 2018 at 06:30 PM
Here is an example on how you can do it. Also ensure you have an EventSystem
object in the scene where you have the button with the onclick action, if you don't it won't fire the onclick action, when you click the button. You can add the EventSystem
object by clicking create in the hierarchy > UI > Event System (https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.html)
public SpriteRenderer spriteRender;
void Start () {
spriteRender.enabled = false;
}
public void EnableSpriteRender() {
spriteRender.enabled = true;
}
// Set spriteRender.enabled to the opposite of the current state
void ToggleSpriteRender() {
spriteRender.enabled = !spriteRender.enabled;
}
No problem. Glad i could help. Good luck with your project.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Inspector vs Script: Component best practice? 1 Answer
Development of the shop? 1 Answer
enable/disable object in heirachy from another object 1 Answer