0
\$\begingroup\$

I have a code that keeps a gameObject between scenes, the theme is that this gameObjecte has as a child a button, which I can change the linerenderer color, besides changing the image of the button according to the selected color and thus keep the data between scenes. then when I change to the next scene I deactivate the button with the meteodo "dehabilitar()" and in the others it is not necessary, then when I change to the next scene I deactivate the button and it is not shown, but when I go back to the next scene and call the function " habilitar() "the button is not displayed. If someone can help me in what I have bad in the code or what I lack?

PS: I did different methods to activate and deactivate the button but the same thing always happens and I leave it like that for a while.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namesp

ace DigitalRubyShared
    {
        public class CambioColor : MonoBehaviour
        {

            public Button mybutton;
            public LineRenderer p1;
            public LineRenderer p2;
            public Sprite colorRojo;
            Color Rojo = new Color(1, 0, 0, 1);
            public Sprite colorAzul;
            Color Azul = new Color(0, 0, 1, 1);
            public static int counter = 0;

            public static bool estado = false;

            public static CambioColor estadoCambioColor;

            void Awake()
            {
                if (estadoCambioColor == null)
                {
                    estadoCambioColor = this;
                    DontDestroyOnLoad(gameObject);
                }
                else if (estadoCambioColor != this)
                {
                    Destroy(gameObject);
                }

            }

            void Start()
            {
                mybutton = GetComponent<Button>();
                p1 = GetComponent<LineRenderer>();
                p2 = GetComponent<LineRenderer>();

                counter = 2;
            }

            public void changeColor()
            {

                counter++;

                if (counter == 1)
                {
                    mybutton.image.overrideSprite = colorAzul;
                    //mybutton.image.sprite = colorAzul;
                    p1.startColor = Azul;
                    p1.endColor = Azul;
                    p2.startColor = Azul;
                    p2.endColor = Azul;

                }
                if (counter == 2)
                {
                    mybutton.image.overrideSprite = colorRojo;
                    //mybutton.image.sprite = colorRojo;
                    p1.startColor = Rojo;
                    p1.endColor = Rojo;
                    p2.startColor = Rojo;
                    p2.endColor = Rojo;

                    counter = 0;
                }


            }

            public void dehabilitar()
            {
                if (estado == false)
                {
//mybutton.enabled = estado;
                    mybutton.gameObject.SetActive(estado);
estado = true;
                }
            }

            public void habilitar()
            {
                if(estado == true)
                {
//mybutton.enabled = estado;
                    mybutton.gameObject.SetActive(estado);
                    estado = false;
                }

            }


            // Update is called once per frame
            void Update()
            {

            }
        }

    }
\$\endgroup\$
4
  • \$\begingroup\$ myButton.interactable = false \$\endgroup\$ – trollingchar May 20 '19 at 6:14
  • \$\begingroup\$ @trollingchar Please don't post answers as comments. If you would like to answer, write a proper answer. \$\endgroup\$ – Philipp May 20 '19 at 11:35
  • \$\begingroup\$ @Philipp that was a suggestion, not an answer. I couldn't understand what is the problem exactly. \$\endgroup\$ – trollingchar May 20 '19 at 12:26
  • \$\begingroup\$ @Philipp What I want to do is hide the button when I change the scene but when I put the scene back where this button was shown before, but I could not achieve it. \$\endgroup\$ – Nicolas Flores Muñoz May 29 '19 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.