Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unity3D invalid token error on official code from documentation

i was trying to set up simple ad system in my game in unity but the rewarded ads script from unity documentation is giving me invalid token error and i have no idea why

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;

public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
    [SerializeField] Button _showAdButton;
    [SerializeField] string _androidAdUnitId = "Rewarded_Android";
    [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
    public string _adUnitId = null;
#if UNITY_IOS
        _adUnitId = _iOSAdUnitId;
#elif UNITY_ANDROID
        _adUnitId = _androidAdUnitId;
#endif

Assets\Scripts\RewardedAdsButton.cs(14,13): error CS1519: Invalid token ‘=’ in class, struct, or interface member declaration
Assets\Scripts\RewardedAdsButton.cs(14,31): error CS1519: Invalid token ‘;’ in class, struct, or interface member declaration

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I doubt this is shown as that in any official code. you can’t just reassign a field on class level. You can’t have this outside of any method or have to do it on the entire field declaration.


This is probably supposed to happen in method like e.g. in Awake

public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
    [SerializeField] Button _showAdButton;
    [SerializeField] string _androidAdUnitId = "Rewarded_Android";
    [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";

    [NonSerialized] public string _adUnitId = null;

    private void Awake ()
    {
#if UNITY_IOS
        _adUnitId = _iOSAdUnitId;
#elif UNITY_ANDROID
        _adUnitId = _androidAdUnitId;
#endif
    }

    ...
}

Or alternatively I would simply use a property like e.g.

public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
    [SerializeField] Button _showAdButton;
    [SerializeField] string _androidAdUnitId = "Rewarded_Android";
    [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";

    public string _adUnitId
    {
        get
        {
#if UNITY_IOS
            return _iOSAdUnitId;
#elif UNITY_ANDROID
            return _androidAdUnitId;
#else
            return null;
#endif
        }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading