Unity AdsManager脚本CS1503错误求助:无法将InterstitialAd转为string
Hey there, let’s sort out this error you’re running into—it’s a simple mix-up with variable names!
The Root Cause
In your RequestFullScreenAd() method, you accidentally tried to initialize a new InterstitialAd using the fullScreenAd variable (which is already an InterstitialAd object) instead of passing the ad ID string (fullScreenAdID) that the constructor requires. That’s why you’re getting this type mismatch error.
Corrected Code
Here’s the fixed version of your class, with the problematic line updated:
using System.Collections; using System.Collections.Generic; using UnityEngine; using GoogleMobileAds.Api; public class AdsManager : MonoBehaviour { public static AdsManager instance; private string appID = "ca-app-pub-3940256099942544~3347511713"; private BannerView bannerView; private string bannerID = "ca-app-pub-3940256099942544/6300978111"; private InterstitialAd fullScreenAd; private string fullScreenAdID = "ca-app-pub-3940256099942544/1033173712"; private void Awake() { if (instance == null) { instance = this; } else { Destroy(this); } } private void Start() { RequestFullScreenAd(); } public void RequestBanner() { bannerView = new BannerView(bannerID, AdSize.Banner, AdPosition.Bottom); AdRequest request = new AdRequest.Builder().Build(); bannerView.LoadAd(request); bannerView.Show(); } public void HideBanner() { bannerView.Hide(); } public void RequestFullScreenAd() { // Fixed: use the string ad ID instead of the InterstitialAd object fullScreenAd = new InterstitialAd(fullScreenAdID); AdRequest request = new AdRequest.Builder().Build(); fullScreenAd.LoadAd(request); } public void ShowFullScreenAd() { if (fullScreenAd.IsLoaded()) { fullScreenAd.Show(); } else { Debug.Log("Full Screen Ad Not Loaded"); } } }
Quick Note
This is a common slip-up when working with ad SDKs—just remember to double-check that you’re passing the correct variable type (string IDs for constructors, object references for other operations) and you’ll avoid this kind of error in the future.
内容的提问来源于stack exchange,提问作者K.Oussama

