I finished my first game weeks ago in Unity; since that time and now, I have been trying to add simple banner ads. Yes, for weeks I’ve been trying to integrate ads. I followed the official RevMob tutorial (http://sdk.revmobmobileadnetwork.com/unity.html#contact), have tried many different things on my own, tried contacting many sources for help (including emailing RevMob devs themselves), and have made literally no progress since day one of this frustrating process. Can someone please help me out here? The problem I’m currently having is this exact error in Unity after adding my banner ad code: "NullReferenceException: Object reference not set to an instance of an object RevMobID.Start() (at Assets/RevMobID.cs:25). Here’s the code in question:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RevMobID : MonoBehaviour
{
private RevMobBanner banner;
private static readonly Dictionary<String, String> REVMOB_APP_IDS = new Dictionary<String, String> ()
{
{“IOS”, “(my ad ID here)”}
};
private RevMob revmob;
void Awake()
{
revmob = RevMob.Start (REVMOB_APP_IDS, “Banner Ad”);
}
void Start()
{
#if UNITY_IPHONE
banner = revmob.CreateBanner(0, 0, Screen.width, Screen.height/10, null, null); // error points here
banner.Show();
#endif
}
}
Hi, I think you need to use him callbacks, changing your script a little
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RevMobID : MonoBehaviour, IRevMobListener
{
private RevMobBanner banner;
[INDENT]private static readonly Dictionary<String, String> REVMOB_APP_IDS = new Dictionary<String, String> () {
{[/INDENT]
"IOS", "(my ad ID here)"}
[INDENT]}
private RevMob revmob;[/INDENT]
[INDENT]void Awake()
{
[INDENT]revmob = RevMob.Start (REVMOB_APP_IDS, "Banner Ad");[/INDENT]
}
void Start()
{
[INDENT]//maybe here revmob just dont started yet[/INDENT]
}
public void SessionIsStarted ()
{
[INDENT]Debug.Log("Session started.");
//add here your banner
#if UNITY_IPHONE
banner = revmob.CreateBanner(0, 0, Screen.width, Screen.height/10, null, null); // error points here
banner.Show();
#endif[/INDENT]
}[/INDENT]
}
1 Like
It worked! I’m not sure why–especially considering the code I had was basically straight from their tutorial page–but I’m no longer getting any errors! Why does moving that block of code from Start to a new, public class fix the error? And why did you include IRevMobListener? I got a bunch of errors so just took that part out, but wow thank you so much for replying!
Glad it worked.
It’s simple, imagine you have this diagram flow for your app for some object:
1- Initiate super new hot object
2- Made necessary alterations
3- Now, I want to get some response from another local (like web). What do I? Simple, Send request
4- Wait for respose
5- Send for all local is waiting for response that answer
6- Done
The step 4 is crucial for right response, otherwise you get wrong message. Well, it’s a bit complicate if you dont need yet implement Interface, but IRevMobListener it just an class with some default functions wich have these feedbacks. When you declared in your class, you saying : Now I can use this functions here to my revMob object. Probably internally revMob send for all revMob objects this functions, including when that have response from web.
So let’s you dont implement this feedback functions, “How” and “When” you get right response? Probably “never”, not in right time.
Here have more about interface : interface keyword - C# reference | Microsoft Learn