Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Godroyah · Oct 04, 2018 at 05:37 AM · mobiletutorialstartoverrideschool

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

198 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges