Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I just can't get a script working!

Discussion in 'Editor & General Support' started by markzareal, Jul 22, 2014.

  1. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    I'm just trying to enable/activate a script from another object. It's as simple as that! But I just can't get it working.
    I asked a few questions on unity answers but none of them helped and people didn't reply to my comments. I have a GUI Texture/Button and it should be activating the other script.I have got no errors in the console, and I couldn't figure out what I have done wrong here. Heres my code(for the button and by the way I'm using javascript) :

    Code (JavaScript):
    1. function OnMouseDown () {
    2. Debug.Log("Clicked!");
    3. GameObject.Find("theobject").GetComponent(thescript).enabled = true;
    4. }
    Does anyone have any ideas? I really need help. Any help is much appreciated. Thanks in advance!
     
    Last edited: Jul 22, 2014
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Does GameObject.Find("the object") return the game object you want/expect it to?

    Does that game object have a script on it called thescript?

    Does GetComponent() return the script you want?

    If all of those are true, then I'd expect your script to work.
     
  3. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    Yes, all of them above. But i have 1 question. I have the OnEnable function on the other script so it won't play by it self when playing the game. Does that cause the problem?
     
  4. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
     
  5. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    Graham has already asked, but are you sure GameObject.Find("the object") returns the object you want? Or does it return null? If I am understanding you correctly, you want another script to activate an inactive object? GameObject.Find only returns active objects.
     
  6. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    I think you first need to declare a variable and put your disabled GameObject on it then enable it...

    ...and this belongs to Scripting sub-forum.

    edit: Oh, is just the component "thescript" that it's disabled. nvm then, that should be working.
     
    Last edited: Jul 22, 2014
  7. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    "thescript" is not disabled, its just a function OnEnable on it.
     
  8. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    Let me just give you guys the whole script:
    Code (JavaScript):
    1. function OnMouseDown () {
    2. Debug.Log("Clicked!");
    3. GameObject.Find("object").GetComponent(script).enabled = true;
    4. }
    and the script thats being should be enabled:
    Code (JavaScript):
    1. var diamond : Sprite;
    2.      var circle : Sprite;
    3.      var triangle : Sprite;
    4.      var square : Sprite;
    5.      private var number : int;
    6.      private var spriteRenderer : SpriteRenderer;
    7.     function Start () {
    8.     number = Random.Range(1,4);
    9.     if(number == 1) {
    10.     spriteRenderer.sprite = diamond;
    11.     }
    12.     else if(number == 2) {
    13.     spriteRenderer.sprite = circle;
    14.     }
    15.     else if (number == 3) {
    16.     spriteRenderer.sprite = triangle;
    17.     }
    18.     else {
    19.     spriteRenderer.sprite = square;
    20.     }
    21.     }
    Do you guys see anything wrong with this code?
     
  9. joni-giuro

    joni-giuro

    Joined:
    Nov 21, 2013
    Posts:
    435
    How can you enable something that's already enabled?
     
  10. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Yes. But you ignored me the first time, so I expect you'll just ignore me this time.

    Does GameObject.Find("object") actually find a game object? Do you have a game object called "object"? Maybe this part of your script fails. (Which means you'll get a null reference exception.)

    Does GetComponent(script) work? In your original post you used thescript and now it's script. What happens if you use double quotes like GetComponent("script")? The argument to GetComponent should be the type, if it's known. Since I don't know what your script is called, it's hard to know what you should use.
     
  11. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    Oh my god, that simple short statement was the best answer I've ever had in a week! Thankyou so much!! All I had to do is disable the script from the object! Can I ask a question. If I press the button, it would just change the sprite non stop. Do you have any idea how to play the script only once every time I press the button?
     
  12. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Sounds like you need to have a bool to track when pressed and set it to true, then catch the release event and set it to false. In the pressed event you would immediately return when the flag is true.
     
  13. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    How do you do that? Sorry I'm pretty new to unity
     
  14. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Something like this (not tested):

    public bool AcceptInput;

    public void OnMouseDown()
    {
    if (this.AcceptInput)
    {
    // Handle input
    this.AcceptInput = false;
    }
    }

    public void OnMouseUp()
    {
    this.AcceptInput = true;
    }
     
  15. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    Do you know how to do it javascript?
     
  16. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  17. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
  18. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    It's just a variable. I don't know unityscript that well, but something like this should work (sorry, at day job so can't test):

    Code (JavaScript):
    1.  
    2. var AcceptInput : bool = true;
    3.  
    4. function OnMouseDown()
    5. {
    6.   if (AcceptInput)
    7.   {
    8.     // Handle input
    9.     AcceptInput = false;
    10.   }
    11. }
    12.  
    13. function OnMouseUp()
    14. {
    15.   AcceptInput = true;
    16. }
    17.  
     
  19. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    Theres no errors(except in javascript bool is boolean but I fixed it) but it didn't really work. It still stayed the same like flashing through sprites.
    Edit: I changed it to this. I think it helped a bit but...
    Code (Javascript):
    1. function OnMouseDown () {
    2. GameObject.Find().GetComponent().enabled = true;
    3. }
    4. function OnMouseUp () {
    5. GameObject.Find().GetComponent().enabled = false;
    6. }
    but it only stopped when I released the touch
     
    Last edited: Jul 25, 2014
  20. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Post the full code from both of your scripts. I'm not able to reproduce the issue as I understand it.
     
  21. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    I already did, its above, but I'll just post it anyways. For the button:
    Code (JavaScript):
    1. function OnMouseDown () {
    2. Debug.Log("Clicked!");
    3. GameObject.Find("object").GetComponent(script).enabled = true;
    4. }
    5. function OnMouseUp () {
    6. GameObject.Find("object").GetComponent(script).enabled = false;
    7. }
    for the script that is being enabled:
    Code (JavaScript):
    1. var diamond : Sprite;
    2.      var circle : Sprite;
    3.      var triangle : Sprite;
    4.      var square : Sprite;
    5.      private var number : int;
    6.      private var spriteRenderer : SpriteRenderer;
    7.     function Update () {
    8.     number = Random.Range(1,4);
    9.     if(number == 1) {
    10.     spriteRenderer.sprite = diamond;
    11.     }
    12.     else if(number == 2) {
    13.     spriteRenderer.sprite = circle;
    14.     }
    15.     else if (number == 3) {
    16.     spriteRenderer.sprite = triangle;
    17.     }
    18.     else {
    19.     spriteRenderer.sprite = square;
    20.     }
    21.     }
     
  22. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
     
  23. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    So, the problem is you are setting the sprite image in the Update() method. While that script is enabled (when the mouse button is held down) it is getting a random number and resetting the image.

    What you want to do is to not use the Update() and instead create (rename basically) another method, say RandomlyChangeSpriteImage() with that logic. Then, you would call that new method from the button script instead of enabling/disabling the second script (such as how I originally described with the AcceptInput Boolean).
     
  24. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    Thankyou So much! You saved my day! I really appreciate it! Your my hero!
     
    Last edited: Jul 26, 2014