0
\$\begingroup\$

I have 4 scenes in my Unity Project. And in each scene I have identical sliders, what I want to do when Advance_Trial is called via a button press, the sliders' value resets to 0. However, this only works for two of the scenes. For remaining two it doesnt work, I checked if Advance_Trial is attached to the button gameobject which calls it and it is (in all scenes). There are no errors messages in the console either. How do I correct it?

Also, when the else portion is executed the sliders are not inactive meaning, the sliders are still visible. This happens for the same two scenes for which the sliders do not reset.

Am I missing something? Or do I need to check something in individual scenes?

Please see my code below.

public Slider[] sliders;
    
    public void Start()
        {
            
            
            sliders = FindObjectsOfType<Slider>() as Slider[];
    
            
     
    
            for (int s = 0; s < sliders.Length; s++) //Resets the sliders
            {
                sliders[s].gameObject.SetActive(false);
            }
    }
     public void Advance_Trial()
        {
            for (int s = 0; s < sliders.Length; s++)
            {
                sliders[s].value = 0;
            }
    
           
            int stimIndex = UnityEngine.Random.Range(0, signals.Count + 1);
            string text = stimIndex.ToString();
            byte[] data5 = Encoding.ASCII.GetBytes(text);
            client.Send(data5, data5.Length, remoteEndPoint);
            Debug.Log("<color=yellow>Signal played is:</color>" + stimIndex);
            Debug.Log("<color=magenta>NUMBER sent to MAX is:</color>" + text);
    
            Debug.Log("<color=magenta>Index removed was :</color>" + signals[stimIndex]);
            signals.RemoveAt(stimIndex);
    
    
            if (signals.Count > index)
            {
    
                Debug.Log("<color=orange>Yay! Keep Listening!</color>");
                index++;
            }
           
                                                            
            else
            {
    
                Debug.Log("<color=green>All Trials finished</color>");
                
                for (int s = 0; s < sliders.Length; s++)
                {
                    sliders[s].gameObject.SetActive(false);
                }
                
                EndSessionPanel.gameObject.SetActive(true);
            }
          
        }
\$\endgroup\$
  • 1
    \$\begingroup\$ Do you miss maybe the event manager in the two scenes that are not working? \$\endgroup\$ – Zibelas Nov 16 at 15:58
  • \$\begingroup\$ Oh Okay! Thank you for bringing that to my attention. I did not check that. \$\endgroup\$ – Tanmayee Pathre Nov 16 at 18:41
  • \$\begingroup\$ @Zibelas, The thing is other other than siders I have buttons which work fine as in they are set to inactive in the else portion. I checked the the event manager it XRUI Input module attached, it is similar to Standalone Input module. How do i check if event manager is working? \$\endgroup\$ – Tanmayee Pathre Nov 17 at 10:16
  • \$\begingroup\$ Easiest is having a new scene with nothing but a new button and a console print. If that is working, add a new button to the scene that are not working with the same console print. If it is not working, compare the event manager and the rest that is different from the scene where it works. If it works in the scene but your slider does not, there is a different problem. \$\endgroup\$ – Zibelas Nov 17 at 10:29
  • \$\begingroup\$ I tried the same thing what you suggested but by using a slider instead of a button. It doesnt work. But now what I did was that I copied the Canvas from working scenes to the non-working ones and it seems to work but what I did is not a solution, it is a hack. \$\endgroup\$ – Tanmayee Pathre Nov 17 at 10:37
0
\$\begingroup\$

If you check the documentation for FindObjectsOfType, you'll find it says:

Objects attached to inactive GameObjects are only included if inactiveObjects is set to true

Without seeing more of your project, I'd suspect that in the scenes where this script does not work as desired, the sliders you want it to edit are either inactive/disabled or not yet present when this script gets its Start() message invoked.

Deleting this line and assigning the sliders you want it to manage in the Inspector is probably the simplest solution. But you can also pass the argument true to include inactive sliders in the search results.

If these two options don't solve your problem, then you should edit your question to include a Minimal Complete Verifiable Example of the problem. A stranger should be able to build a scene in a new, empty Unity project, using only the instructions present in your question, and successfully reproduce the problem you're observing. Once you can get to that point, we can test alternative solutions to be sure they'll solve your problem.

| improve this answer | |
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.