Cannot Override Start() function from MainMenuBehavior w/code in PauseScreenBehavior
This is the first time I have ever needed to deal with Overriding, and I am doing it for a class on building Mobile Games through John Doran's book Unity 2017 Mobile Game Development. Currently on Ch.6 dealing with In-Game Purchases. From reading around the internet I understand generally what an override is supposed to do and that the Start() function you are trying to override with needs to be both protected and virtual, which as far as I can tell is covered. I've been wracking my brain and hopping around the internet trying to find a solution to no avail. I need to turn in my current iteration by 9:30 this morning and I'm about at my wits end; my professor is away on a trip and has specifically noted that his access to email will be spotty at best, which means I'm going to be stuck with faulty code until at least next week if I can go over it with him. If anyone has thoughts or input on what I might be doing wrong I'd definitely appreciate it.
I apologize in advance if what I post below this is formatted oddly.
The script below whose Start() I'm trying to override with
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; //LoadScene
public class MainMenuBehavior : MonoBehaviour {
///<summary>
/// Will load a new scene upon being called
/// </summary>
/// <param name="levelName"> The name of the level we want to go to
/// </param>
public void LoadLevel(string levelName)
{
SceneManager.LoadScene(levelName);
if UNITY_ADS
if(UnityAdController.showAds) //is this right?
{
//Show an ad
UnityAdController.ShowAd();
}
endif
}
public void DisableAds()
{
UnityAdController.showAds = false;
// Used to store that we shouldn't show ads
PlayerPrefs.SetInt("Show Ads", 0);
}
protected virtual void Start()
{
//Initialize the showAds variable
UnityAdController.showAds = PlayerPrefs.GetInt("Show Ads", 1) == 1;
}
}
The script below that is trying to initiate the override.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; //Scenemanager
public class PauseScreenBehavior : MonoBehaviour {
public static bool paused;
[Tooltip("Reference to the pause menu object to turn on/off")]
public GameObject pauseMenu;
/// <summary>
/// Reloads our current level, effecitvely "restarting" the game
/// </summary>
public void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
/// <summary>
/// Will turn our pause menu on or off
/// </summary>
/// </param name="isPaused"></param>
public void SetPauseMenu(bool isPaused)
{
paused = isPaused;
// If the game is paused, timeScale is 0, otherwise 1
Time.timeScale = (paused) ? 0 : 1;
pauseMenu.SetActive(paused);
}
public void LoadLevel()
{
SceneManager.LoadScene("MainMenu");
}
protected override void Start()
{
//Initialize Ads if needed
base.Start();
paused = false;
//If no ads at all, just unpause
if !UNITY_ADS
SetPauseMenu (false); #else
//If we support ads but they're removed, unpause as well
if (!UnityAdController.showAds)
{
SetPauseMenu(false);
}
endif
}
}
Thank you again for any input or thoughts you put up here; I'll be sure to check in in a few hours.
,So I am entirely new to the concept of overriding; I am following along in Doran's book Unity 2017 Mobile Game Development in Ch.6 regarding In-App Purchases. I've tried looking to see what I might be missing but requirements such as having the Start() function I'm trying to pull both protected and virtual have been met as far as I can tell. I have to have something to show by tomorrow at 9:30 and I'm pretty much at a loss at this point as to what is wrong. My code is as follows:
This is the code from which I'm trying to pull the Start() function
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; //LoadScene
public class MainMenuBehavior : MonoBehaviour {
///<summary>
/// Will load a new scene upon being called
/// </summary>
/// <param name="levelName"> The name of the level we want to go to
/// </param>
public void LoadLevel(string levelName)
{
SceneManager.LoadScene(levelName);
if UNITY_ADS
if(UnityAdController.showAds) //is this right?
{
//Show an ad
UnityAdController.ShowAd();
}
endif
}
public void DisableAds()
{
UnityAdController.showAds = false;
// Used to store that we shouldn't show ads
PlayerPrefs.SetInt("Show Ads", 0);
}
protected virtual void Start()
{
//Initialize the showAds variable
UnityAdController.showAds = PlayerPrefs.GetInt("Show Ads", 1) == 1;
}
}
This code below is that which is trying to make the override
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; //Scenemanager
public class PauseScreenBehavior : MonoBehaviour {
public static bool paused;
[Tooltip("Reference to the pause menu object to turn on/off")]
public GameObject pauseMenu;
/// <summary>
/// Reloads our current level, effecitvely "restarting" the game
/// </summary>
public void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
/// <summary>
/// Will turn our pause menu on or off
/// </summary>
/// </param name="isPaused"></param>
public void SetPauseMenu(bool isPaused)
{
paused = isPaused;
// If the game is paused, timeScale is 0, otherwise 1
Time.timeScale = (paused) ? 0 : 1;
pauseMenu.SetActive(paused);
}
public void LoadLevel()
{
SceneManager.LoadScene("MainMenu");
}
protected override void Start()
{
//Initialize Ads if needed
base.Start();
paused = false;
//If no ads at all, just unpause
if !UNITY_ADS
SetPauseMenu (false); #else
//If we support ads but they're removed, unpause as well
if (!UnityAdController.showAds)
{
SetPauseMenu(false);
}
endif
}
}
If anyone has any thoughts I'd appreciate any replies. I'm pretty much prepared to turn this in as-is if I need to and work out the kinks with my professor, though he is out this week which means that I'm going to have to work with unworkable code for the next chapter until he returns next week. He has already mentioned reaching him via email will be spotty so email exchanges back and forth may or may not work for the purpose of trying to figure out what I did wrong.
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612175732im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Where to start my project? 1 Answer
How can I make an object move normally with a UI Button 0 Answers
how to have continuous touch on a mobile device? 0 Answers
my game is freezing! 0 Answers
iOS Resolutions iPhone and iPad 0 Answers