How change Button Sprite when i pause my game ( SOLVED ERROR )

Hello anyone can help me …How change Button Sprite when i pause my game
I try this script but it is not working :frowning:

void Pause ()
    {

                  
        if (canpause) {


            Sprite newSprite = sprite;
            Image theImage = GameObject.Find("P2").GetComponent<Image>();
            theImage.sprite = newSprite;

            Time.timeScale = 0;
            canpause = false;
        }

        else
        {

            Time.timeScale=1;
            canpause = true;

        }
    }

Please take a look at this page and update your post (and future posts): Using code tags properly - Unity Engine - Unity Discussions

That will allow you to post code on the forums in a more readable and nicer format :slight_smile:

Okay, there’s no need to use a ‘newSprite’ variable if you already have the sprite linked/referenced. That wouldn’t make it not work, it’s just not required. Do you have any errors when you’re doing this?

You should also make a variable for your “P2” game object and drag & drop it in the inspector if you can.
If you cannot, you should at least look it up at cache it in Start() and use the variable later, instead of repeatedly using ‘Find’ which is a slow method.

1 Like

Hello sorry i’m new … Can you write correct script for me !! C#

Well, I asked if there are any errors, because if everything is connected & referenced correctly, the script should be working.
Does the method get called? Do you know it’s running for sure?

Yeah i get error :frowning: … do u have other way to change button sprite if clicked !!

lol well what is the error?! haha. If you have a script, and it has an error, you have to look into the error… that’s what it’s there for, man! :slight_smile: C’mon… :slight_smile:

1 Like

hahaha Allright wait i will try see what is the error … I just want your way because maybe it is much better :wink:

Well, my way is the same way as the one you wrote, including the adjustments I mentioned in my previous post.
I mean I created a very similar script and even tried it and it’s working…Think I have since deleted it, but I’m sure that’s because it was right and I didn’t need it lol :slight_smile:

1 Like
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PauseScript : MonoBehaviour {

    public Button PauseGame;
    public bool canpause;
    public Sprite sprite;
   
    void Start () {
 
        canpause = true;
        Button PauseG = PauseGame.GetComponent<Button> ();
        PauseG.onClick.AddListener(Pause);

    }
 
    void Pause ()
    {


        if (canpause) {

            Sprite newSprite = sprite;
            Image theImage = GameObject.Find ("P2").GetComponent<Image> ();
            theImage.sprite = newSprite;

            Time.timeScale = 0;
            canpause = false;


        }

        else
        {

            Time.timeScale=1;
            canpause = true;

        }
    }
}

NullReferenceException: Object reference not set to an instance of an object
PauseScript.Pause () (at Assets/Scripts/PauseScript.cs:26)
UnityEngine.Events.InvokableCall.Invoke (System.Object args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153)
UnityEngine.Events.InvokableCallList.Invoke (System.Object parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:630)
UnityEngine.Events.UnityEventBase.Invoke (System.Object parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:765)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()

I WANT CHANGE TO THIS P2

k man well that’s a problem. Remove that line with Find “P2” thing. Remove the “newsprite” line entirely (unnecessary).
make a new public variable: public Sprite newSprite; (and drag the P2 into that in the inspector)
Then just try on the line :

theImage.sprite = newSprite;

I think that’s it.

Note: You can’t “GameObject.Find” an asset. You can reference your sprite, in this case, in a variable in the inspector, or use Resources.Load() if it’s in the right folder… but you can’t just call GameObject.Find on the name of an asset.

1 Like
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PauseScript : MonoBehaviour {

    public Button PauseGame;
    public bool canpause;
    public Sprite newSprite;

    // Use this for initialization
    void Start () {
   
        canpause = true;
        Button PauseG = PauseGame.GetComponent<Button> ();
        PauseG.onClick.AddListener(Pause);

    }
   
    void Pause ()
    {


        if (canpause) {


            theImage.sprite = newSprite;

            Time.timeScale = 0;
            canpause = false;


        }

        else
        {

            Time.timeScale=1;
            canpause = true;

        }
    }
}

NOW I GET THIS ERROR

Assets/Scripts/PauseScript.cs(27,25): error CS0103: The name `theImage’ does not exist in the current context

Oh, sorry man. .that’s my bad.
Just use : GetComponent().sprite = newSprite;
:slight_smile: Oops.

1 Like

AMAZING BROTHER IT IS WORKING NOW

*********** THANK YOU SO MUCH **********

ROFL. you’re most welcome, man :slight_smile: Cheers, enjoy your game-making …!

1 Like

Still long way heheheh now i should think how make my player camera swipe up , down, left , right when touch my phone screen :wink: ANY IDEA
I’m trying create shooting game :smile:

Uh huh :slight_smile: There’s always something “next” to do :wink: