Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Anthomaxyd · Jun 24, 2020 at 03:49 PM · classstartvoid

My void Start won't work

Hello everyone. I'm basically a noob on unity, so i need your help on this one. I'm modifying a code i scrape from a codemonkey tutorial. They idea is to save a value at the end of each game, and compare this value to the previous in order for the player to understand is improvement compared to his previous play. My problem is that my second void sart (in the Highscores Private class) won't start when i launch the game. If everyone have an explanation i will gladly hear it. Thanks a lot, the code is below. using System.Collections; using System.Collections.Generic; using UnityEngine; using CodeMonkey; using CodeMonkey.Utils; using System.IO;

[System.Serializable] public class GameHandler : MonoBehaviour {

 private Transform entryContainer;
 private Transform entryTemplate;
 private int MaxObjCount = 2;
 private List<Transform> highscoreEntryTransformList;


 private void Awake()
 {
     
  

     

     string jsonString = PlayerPrefs.GetString("highscoreTable");
     Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString);
     
     if (highscores == null)
     {
         // There's no stored table, initialize
         Debug.Log("Initializing table with default values...");
         AddHighscoreEntry(0);
         AddHighscoreEntry(0);
      
         // Reload
         jsonString = PlayerPrefs.GetString("highscoreTable");
         highscores = JsonUtility.FromJson<Highscores>(jsonString);
     }

    

     highscoreEntryTransformList = new List<Transform>();
   

 }

 


 public static void AddHighscoreEntry(float score)
 {
     



     // Create HighscoreEntry
  // PlayerPrefs.DeleteAll();
     HighscoreEntry highscoreEntry = new HighscoreEntry { score = score};
     

     // Load saved Highscores
     string jsonString = PlayerPrefs.GetString("highscoreTable");
     Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString);

     if (highscores == null)
     {
         // There's no stored table, initialize
         highscores = new Highscores()
         {
             highscoreEntryList = new List<HighscoreEntry>()
         };
     }
     
     // Add new entry to Highscores
     highscores.highscoreEntryList.Add(highscoreEntry);

     /*  while (highscoreEntryTransformList.Count >MaxObjCount)
       {
           if (highscoreEntryTransformList[0] != null)
               Destroy(highscoreEntryTransformList[0].highscores);
           highscoreEntryTransformList.RemoveAt(0);
       }*/

     // Remove Old entry to Highscores

     // Save updated Highscores
 
     string json = JsonUtility.ToJson(highscores);
     PlayerPrefs.SetString("highscoreTable", json);
     PlayerPrefs.Save();
    // Debug.Log(PlayerPrefs.GetString("highscoreTable"));
    
 }

 
 private class Highscores : MonoBehaviour
 {
     public List<HighscoreEntry> highscoreEntryList;
   public void Start()
     {
         Debug.Log(highscoreEntryList.Count);
         DeletePreviousScore();
         
     }
 

     private void DeletePreviousScore()
     {

         if (highscoreEntryList.Count > 2)
         {
             highscoreEntryList.RemoveAt(0);

         }
     }

 }

 /*
  * Represents a single High score entry
  * */
 [System.Serializable]
 private class HighscoreEntry
 {
     public float score;
    
    
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ShadyProductions · Jun 24, 2020 at 04:13 PM

You have your Highscores monobehaviour class inside another monobehaviour class, how do you expect it to work?

Comment
Add comment · Show 2 · Share
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
avatar image Anthomaxyd · Jun 25, 2020 at 09:33 AM 0
Share

First of all, thank you for your reply.The thing is that if i don't use monobehaviour with this class, unity won't recognize the Start void, that's why i make this mistake. Any idea on how to solve this? I have first try to move the Highscores class in a different script. But when i do that, the script won't recognize the HighscoreEntry term in the public List highscoreEntryList; That's why i try to make it all work on the sazme script. Again thank you for your time

avatar image ShadyProductions Anthomaxyd · Jun 27, 2020 at 10:23 AM 0
Share

You need to make the class public and make sure you import the namespace it is part off.

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

131 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

Related Questions

Full list of predefined voids? 2 Answers

Initialising List array for use in a custom Editor 1 Answer

Avoid function being called every frame (on Start void) 1 Answer

A Script error With Void Start 2 Answers

start called directly after instantiate? 1 Answer


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