Search Unity

Enable/Disable ScrollRect by script ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Skribble, Oct 20, 2014.

  1. Skribble

    Skribble

    Joined:
    Oct 29, 2012
    Posts:
    16
    Hello there guys,

    I've been fighting with this for days and i haven't been able to find the solution, also been doing research but nothing has come so far.

    My issue is, i have a GameObject and i want to Disable the ScrollRect component by script.. but im not able to do it, here's what i've been doing so far..



    i can't get the component ... i've been trying to set the test1 as GameObject and also as RectTransform, but havent got nothing so far.. also i can get but nothing else from there.

    There's anyone who can bring some light please? i'll really appreciate it !

    Thank you!
     
  2. Zebra-Sin

    Zebra-Sin

    Joined:
    Feb 9, 2014
    Posts:
    22
    Hello

    ScrollRect is in UnityEngine.UI

    You can do (C#):
    MyGameObject.GetComponent<UnityEngine.UI.ScrollRect>().SetActive(false);

    Or (C#):

    using UnityEngine.UI;

    class MyClass
    {

    void MyFunction()
    {

    MyGameObject.GetComponent<ScrollRect>().SetActive(false);
    }
    }
     
  3. Skribble

    Skribble

    Joined:
    Oct 29, 2012
    Posts:
    16
    OMG Thanks a lot !

    im going to give it a try.

    Really appreciate it man !
     
  4. Zebra-Sin

    Zebra-Sin

    Joined:
    Feb 9, 2014
    Posts:
    22
    You're welcome ;-)
     
  5. Skribble

    Skribble

    Joined:
    Oct 29, 2012
    Posts:
    16
    Hey Zebra Sin, im just trying it out but doesn't seems to work any of the ways, or there's something im doing wrong ?

    auxScroll is a GameObject.

     
  6. Zebra-Sin

    Zebra-Sin

    Joined:
    Feb 9, 2014
    Posts:
    22
    Hey Skribble
    Can you post more code related please ? Just like that, it would be working without problem.
    Is it C# ? (I dont't code in another language in Unity)
     
  7. Skribble

    Skribble

    Joined:
    Oct 29, 2012
    Posts:
    16
    Hello Zebra Sin,

    Yes it's C#

    Here's the related code:



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

    public class Photo : MonoBehaviour
    {
    private bool newPivot=false;
    private Vector3 scale;
    private int aux;
    public GameObject auxScroll;


    void On_Cancel2Fingers( Gesture gesture )
    {
    if (gesture.touchCount >= 0)
    {
    auxScroll.GetComponent<UnityEngine.UI.ScrollRect>().SetActive(false);
    auxScroll.GetComponent<ScrollRect>().SetActive(false);
    newPivot=true;
    }
    }
    }
     
  8. Skribble

    Skribble

    Joined:
    Oct 29, 2012
    Posts:
    16
    Also here's the errors i'm getting for each one of the lines, this maybe can help






    Thanks in advance ! really appreciate it !
    -Brian.
     
  9. Zebra-Sin

    Zebra-Sin

    Joined:
    Feb 9, 2014
    Posts:
    22
    okay, thanks, it is helpfull.
    I mistake for the SetActive(), I confused with gameobject, for a component, it's the property "enabled = false".
    For the second error, I don't really know. maybe a compilor problem or simply error related to the first error (really, I don't know, I'm using Visual Studio for C# editing)..

    So, here is your corrected code:

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

    public class Photo : MonoBehaviour

    {
    private bool newPivot = false;
    private Vector3 scale;
    private int aux;

    public GameObject auxScroll;

    void On_Cancel2Fingers(Gesture gesture)
    {
    if (gesture.touchCount >= 0)
    {
    // Use this line if you don't use "using UnityEngine.UI;" at top
    auxScroll.GetComponent<UnityEngine.UI.ScrollRect>().enabled = false;
    // Else, use this line:
    auxScroll.GetComponent<ScrollRect>().enabled = false;
    newPivot = true;
    }
    }
    }
     
    billdosk and Skribble like this.
  10. Skribble

    Skribble

    Joined:
    Oct 29, 2012
    Posts:
    16
    OMG worked !

    With this solution auxScroll.GetComponent<ScrollRect>().enabled = false; is working perfect !

    Thank you so much Zebra Sin !!

    also tried the another solution, but somehow unity is not recognizing the .UI part.. but no problem.

    also just as a comment, its really weird but im still seeing this line

    auxScroll.GetComponent<ScrollRect>().enabled = true;

    in red as if there were errors on it, however the compiler shows none and works perfect.. that's weird.

    Thanks again mate !
     
  11. Zebra-Sin

    Zebra-Sin

    Joined:
    Feb 9, 2014
    Posts:
    22
    You are welcome, again ;)

    maybe has you misinstall multi unity within beta and non beta ? you must separate folders used by Unity and Mono, else unity installer erase old mono, and mono is confused within these versions.
    If not, I don't know, I don't use mono ^^
     
  12. Skribble

    Skribble

    Joined:
    Oct 29, 2012
    Posts:
    16
    well that totally makes sense.. yeah that's true, i install / uninstall Unity always in the default folder.
    thanks you're da man !