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 scottsniper77 · Jul 27, 2017 at 10:12 PM · dontdestroyonloadpropertiesgetset

Destroy on load/Properties Issue

So I have been trying and trying to resolve this issue. Basically the integer value for the color is assigned to theUI script in one scene and It is defiantly assigned. Next scene the Material changer script says that the color value is 0. Help me.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class TheUI : MonoBehaviour {

 private int color;
 public Button multiplayer;
 public Text multiplayertext;
 public Button exit;
 public Text exittext;
 public Text ColorText;
 public Dropdown ColorBox;
 public Text ColorBoxText;
 public Image ColorBoxArrow;

 public int ColorProperty
 {
     get
     {
         return color;
     }
 }

 private void Awake()
 {
     DontDestroyOnLoad(transform.gameObject);
 }

 private void Start()
 {
     ColorText.enabled = false;
     ColorBox.enabled = false;
     ColorBoxText.enabled = false;
     ColorBoxArrow.enabled = false;
 }

 public void ColorSelect()
 {
     multiplayer.enabled = false;
     exit.enabled = false;
     multiplayertext.enabled = false;
     exittext.enabled = false;
     ColorText.enabled = true;
     ColorBox.enabled = true;
     ColorBoxText.enabled = true;
     ColorBoxArrow.enabled = true;
 }

 public void Color()
 {
     color = ColorBox.value;
     Debug.Log("the color is" + color);
 }

 public void Exit()
 {
     Application.Quit();
 }

}

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MaterialChanger : MonoBehaviour {
 
     public int color;
     TheUI myUI = new TheUI();
 
     void Update () {
         color = myUI.ColorProperty;
         Debug.Log(color);
         if (color == 4)
         {
             gameObject.GetComponent<Renderer>().material.color = Color.blue;
         }
         if (color == 3)
         {
             gameObject.GetComponent<Renderer>().material.color = Color.red;
         }
         if (color == 2)
         {
             gameObject.GetComponent<Renderer>().material.color = Color.yellow;
         }
         if (color == 1)
         {
             gameObject.GetComponent<Renderer>().material.color = Color.green;
         }
     }
 }


Comment
Add comment · Show 1
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 scottsniper77 · Jul 27, 2017 at 10:16 PM 0
Share

I also get no errors stating that anything is wrong apart from this one:

 You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword.  This is not allowed.  $$anonymous$$onoBehaviours can only be added using AddComponent().  Alternatively, your script can inherit from ScriptableObject or no base class at all
 UnityEngine.$$anonymous$$onoBehaviour:.ctor()
 TheUI:.ctor()
 $$anonymous$$aterialChanger:.ctor() (at Assets/$$anonymous$$aterialChanger.cs:8)

regarding this line of code:

 TheUI myUI = new TheUI();

which is in the material changer script on line 8.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by · Jul 27, 2017 at 10:24 PM

You should not instantiate the the class TheUI. Instead you need to find the reference for the already created one in the scene.

 public class MaterialChanger : MonoBehaviour {
  
      public int color;
      TheUI myUI;
 
 void Start()
 {
         myUI = Object.FindObjectOfType<TheUI>(); //You can also find the gameObject and then getting it's component
 }
 
  ....
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
avatar image
2

Answer by FortisVenaliter · Jul 27, 2017 at 10:16 PM

...Yeah, of course it does. You don't set the reference to the instance of the first object, you set it to 'new TheUI()' which would be left completely blank since it wouldn't even be attached to a GameObject.

You need to set the reference properly instead.

Comment
Add comment · Show 3 · 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 scottsniper77 · Jul 27, 2017 at 10:23 PM 0
Share

either im a bit dumb and i dont know what you said or dosen't TheUI stay on the object its on as i used dontdestroyonload which stops the object from being destroyed

avatar image FortisVenaliter scottsniper77 · Jul 27, 2017 at 10:27 PM 0
Share

Yes, you do set it to not destroy on load. But... look at it this way:

You create the first TheUI properly, on a GameObject. Let's call this TheUI-A.

Then the next scene loads, and $$anonymous$$aterialChanger needs a reference to TheUI. But rather than finding TheUI-A, it says to create a new one (hence the 'new' keyword), which we'll call TheUI-B.

Now, TheUI-A has all the values, but no one is using them. But TheUI-B is being used by your script, but it's brand new and has none of it's values set.

avatar image scottsniper77 · Jul 27, 2017 at 10:27 PM 0
Share

Thank you for helping

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

69 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

Related Questions

C sharp get and set problem: "name doesn not exist in the current context 1 Answer

Modify Built-In Class Variables Get/Set functions 1 Answer

Getter and Setter Question! 1 Answer

Character Select Button Putting Info into GameManager Script - Is this Possible? 0 Answers

Get Set Return method, properties stuff? 2 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