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 nycucumber · Oct 10, 2017 at 03:16 AM · inspectorserialization

Can't find instance for Serializable class public property.

i think this has something to do with Serializable but i don't know enough to say how to fix this.

here is my script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ControlPanel : MonoBehaviour {

 /* 
  * 
  * 
  *  c  l  a  s  s  e  s
  * 
  * 
  */


  public abstract class BoolControl {
     protected string playerprefName;
     public Text buttonText;
     public Button btn;
     public bool defaultState;
     protected bool state {
         get
         {
             return state;
         }
         set {
             if(value){
                 buttonText.text = "OFF";
                 ColorBlock cb = btn.colors;
                 cb.normalColor = Color.blue;
                 btn.colors = cb;
             }else{
                 buttonText.text = "ON";
                 ColorBlock cb = btn.colors;
                 cb.normalColor = Color.black;
                 btn.colors = cb;
             }
             state = value;
         }
     }

     public BoolControl () {
         btn.onClick.AddListener(()=>ToggleState());
         Debug.LogError("added event listener");
     }

     public void ToggleState()
     {
         Debug.LogError("State change");
        
         state = !state;
         ApplyChanges();
     }

     public abstract void ApplyChanges();
     
     public void SavePlayerpref()
     {
         PlayerPrefs.SetInt(playerprefName, System.Convert.ToInt32(state));
     }
     public void ReadPlayerpref()
     {
         state = System.Convert.ToBoolean(PlayerPrefs.GetInt(playerprefName, System.Convert.ToInt32(defaultState)));
     }
 }

 [System.Serializable]
 public class Syphon : BoolControl
 {
     public syphonController script;

     public Syphon():base(){
         playerprefName = "SyphonState";
     }

     public override void ApplyChanges(){
         
     }
 }

 [System.Serializable]
 public class Hue : BoolControl {
     
     public HueLightController script;


     public Hue():base(){
         playerprefName = "HueState";
     }

     public override void ApplyChanges()
     {
         script.on = state;
         if(state){
             
         }else{
             
         }
     }
 }

 [System.Serializable]
 public class Resolution {
     string[] playerprefName = { "resolutionx", "resolutiony" };
     public SuperWideScreen SWC;
     public Text txtAspectRatioX;
     public Text txtAspectRatioY;
     public Text txtCurrentRes;
     public Button btnConfirm;

     Vector2 res;
     public Vector2 defaultRes;


     public void ApplyChanges(){
         //SWC.SetSize(res);
     }

     public void SavePlayerpref(){
         PlayerPrefs.SetFloat(playerprefName[0], res.x);
         PlayerPrefs.SetFloat(playerprefName[1], res.y);
     }

     public void ReadPlayerpref(){
         res.x = PlayerPrefs.GetFloat(playerprefName[0], defaultRes.x);
         res.y = PlayerPrefs.GetFloat(playerprefName[1], defaultRes.y);
     }
 }

 /*
  * 
  * 
  * 
  * 
  *  m  e  t  h  o  d  s
  * 
  * 
  * 
  */

 public Resolution resolutionControl;
 public Syphon syphonControl;
 public Hue hueControl;



 private void SaveStates() {
     
 }

 public void UpdateStates(){
     SaveStates();
 }

 private void ReadStates() {
     resolutionControl.ReadPlayerpref();
     syphonControl.ReadPlayerpref();
     hueControl.ReadPlayerpref();
 }

 private void setColors(){
     
 }

 //void Awake () {

 //}

 // Use this for initialization
 void Start () {
     resolutionControl = new Resolution();
     hueControl = new Hue();
     syphonControl = new Syphon();
     ReadStates();
     resolutionControl.ApplyChanges();
     syphonControl.ApplyChanges();
     hueControl.ApplyChanges();
 }
 
 // Update is called once per frame
 void Update () {
     
 }


}

I have linked the Button to the field Btn ,( see pic attached) but when I run the program, it shows me that it can't find btn.

alt text

NullReferenceException: Object reference not set to an instance of an object ControlPanel+BoolControl..ctor () (at Assets/ControlPanel/ControlPanel.cs:46)

screen-shot-2017-10-09-at-111325-pm.png (37.3 kB)
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
1
Best Answer

Answer by FortisVenaliter · Oct 10, 2017 at 06:58 PM

In the constructor? How is the value expected to be set when the object is just now being instantiated?

No values are set by the time the constructor is called, which is why we have the Start() function. Your code should work just fine from there.

Comment
Add comment · 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

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

121 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

Related Questions

UnityEvent Generic (Inspector Serialization) 0 Answers

public bool not showing in inspector 1 Answer

My variables are not showing in the inspector 1 Answer

How to save a two-dimensional array, as part of the variable inspector? 0 Answers

UIElements: problems with inserting and deleting obejcts in an array 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