Check bool BEFORE destroying?

Hi Unity Engineers!

I am trying to adjust a piece of code, and I’m hoping someone can double check to see if I’m doing this right? The code compiles, but I am not seeing what I imagine the desired result should be.

In the below scenario, a banner ad is popped up (using Prime31’s Tapjoy plugin) if a node has a “Show Ad” box checked.

That part works fine. The problem comes when I try to destroy that ad. If there’s an ad there, it works GREAT! However, if that “Show Ad” box was not checked, and no ad delivered, then the game crashes :stuck_out_tongue: (essentially I cam calling destroyBanner(); on something that is not there).

So.

I have made (I think) the “Show Ad” box set the AdOn bool to “true” if there is an ad called, otherwise it’s set as false.

In the below snippet, I am trying to get the code to check to see if the AdOn bool is true, then fire the destroyBanner(); and if the AdOn is false, it does nothing.

Am I on the right track?

Any help would be greatly appreciated!


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

public class Journal : MonoBehaviour
{
    //UI State
    enum JState
    {
        Common,
        Inventory,
        Message,
    };
    
    //Public members
    public GUISkin m_skin;
    public Rect messageTextureRect;
    
    //Private members
    private JState m_state = JState.Common;
    
    private string m_currText;
    private bool m_run = true;
    const float g_speed = 50.0f;
    private Texture m_icon;
    private string m_label;
    private Texture messageTexture;
    
    private string m_message = null;
    private bool AdOn = false;
    
    //Use this for initialization of Android
    void Start ()
    {
        SetRunText("");
        TapjoyAndroid.init("SECRET", "SOSECRET", true);
        
        TapjoyAndroid.useScreenDensity(true);
        TapjoyAndroid.getFeaturedApp();
        TapjoyAndroid.getDisplayAd();
    }
    
    public void getDisplayAdResponseFailed(string error){} 
    
    public void getDisplayAdResponse(string text){}
    
    //Below sets the
    public void OnLocationChanged(NodeScript node)
    {
        if (node.m_Text!=null && node.m_Text!="" && !node.IsDone)
        {
            m_message = node.m_Text;
            m_icon = node.m_Icon;
            m_label = node.m_Label;
            node.SetAsDone();
            m_state = JState.Message;
            messageTexture = node.messageTexture;
            
            if(node.showAd)
                //TapjoyConnectPlugin.ShowAd();
                //TapjoyAndroid.showFeaturedApp();
                //TapjoyAndroid.showOffers();
                TapjoyAndroid.getDisplayAd();
                TapjoyAndroid.showDisplayAd(0,400);
                AdOn = true;
                Debug.Log("AdOnGO");
        }
    }
    
    void SetRunText(string text)
    {
        m_run = true;
        m_currText = text;
        m_skin.customStyles[0].contentOffset = new Vector2( Screen.width, 0 );
    }
    
    void OnGUI ()
    {
        GUI.Box(new Rect (0, Screen.height-40, Screen.width, 30), m_currText, m_skin.customStyles[0]);
        switch (m_state)
        {
            case JState.Common:
                if (GUI.Toggle(new Rect (Screen.width-50,Screen.height-50,40,40), false, "", m_skin.toggle))
                {
                    m_state = JState.Inventory;	
                }
                break;
                
            case JState.Inventory:
                GUI.Box(new Rect((int)(Screen.width*0.6), 10, (int)(Screen.width*0.4-10), Screen.height - 70), m_message, m_skin.box);				
                if(messageTexture != null)
                    GUI.DrawTexture(new Rect(messageTextureRect.x * Screen.width/320.0f, messageTextureRect.y * Screen.height / 480.0f, messageTextureRect.width * Screen.width / 320.0f, messageTextureRect.height * Screen.height / 480.0f), messageTexture);
                if (m_icon!=null || m_label!=null)
                {
                    GUIContent ctv = new GUIContent();
                    ctv.text = " " + m_label;
                    ctv.image = m_icon;
                    GUI.Label(new Rect((int)(Screen.width*0.6) + 15, 25, 180, 40), ctv, m_skin.label);
                }
                if (!GUI.Toggle(new Rect (Screen.width-50,Screen.height-50,40,40), true, "", m_skin.toggle))
                {
                    m_state = JState.Common;
                }
                break;
            case JState.Message:
                GUI.Box(new Rect((int)(Screen.width*0.6), 10, (int)(Screen.width*0.4-10), Screen.height - 70), m_message, m_skin.box);
                if(messageTexture != null)
                    GUI.DrawTexture(new Rect(messageTextureRect.x * Screen.width/320.0f, messageTextureRect.y * Screen.height / 480.0f, messageTextureRect.width * Screen.width / 320.0f, messageTextureRect.height * Screen.height / 480.0f), messageTexture);
                if (m_icon!=null)
                //This controls the Icon and title location in the clue text box
                {
                    GUIContent ctv = new GUIContent();
                    ctv.text = " " + m_label;
                    ctv.image = m_icon;
                    GUI.Label(new Rect((int)(Screen.width*0.6) + 15, 25, 180, 40), ctv, m_skin.label);
                    //GUI.Label(new Rect((int)(Screen.width*0.6) + 65, 25, 60, 40), m_label);
                }
                if (!GUI.Toggle(new Rect (Screen.width-50,Screen.height-50,40,40), true, "", m_skin.toggle))
                {
                    m_state = JState.Common;
                    {
                        if(AdOn == true) 
                        {
                            TapjoyAndroid.destroyBanner();
                            Debug.Log("DESTROY");
                            AdOn = false;
                        }
                    }
                }
                break;
                if (m_run)
                {
                    if (m_skin.customStyles[0].contentOffset.x<-m_currText.Length*10)
                        m_skin.customStyles[0].contentOffset = new Vector2( Screen.width, 0 );
                    else
                        m_skin.customStyles[0].contentOffset = new Vector2( m_skin.customStyles[0].contentOffset.x - g_speed * Time.deltaTime, 0 );
                    //print(m_skin.customStyles[0].contentOffset.x);
                }
        }
        
    }
    
    public bool ShouldLockGame()
    {
        return m_state != JState.Common;
    }
    
    public void ShowInventoryItem(InventoryItemScript d)
    {
        m_message = d.Description;
        m_icon = d.Icon;
        m_label = d.PrettyName;
        m_state = JState.Inventory;
        messageTexture = d.messageTexture;
    }
    
    public void AddToInventory(InventoryItemScript item)
    {
        ShowInventoryItem(item);
    }
}

This may be just because the formatting of your displayed code and gone a bit awry but it looks like the problem is with your code here:

if(node.showAd)
    //TapjoyConnectPlugin.ShowAd();
    //TapjoyAndroid.showFeaturedApp();
    //TapjoyAndroid.showOffers();
    TapjoyAndroid.getDisplayAd();
    TapjoyAndroid.showDisplayAd(0,400);
    AdOn = true;
    Debug.Log("AdOnGO");

You’re missing some brackets so that only the first line of code (TapjoyAndroid.getDisplayAd():wink: is being included under the if statement and the rest of the code will always be executed (including the AdOn = true;) It should look like:

if(node.showAd)
{
    //TapjoyConnectPlugin.ShowAd();
    //TapjoyAndroid.showFeaturedApp();
    //TapjoyAndroid.showOffers();
    TapjoyAndroid.getDisplayAd();
    TapjoyAndroid.showDisplayAd(0,400);
    AdOn = true;
    Debug.Log("AdOnGO");
}