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 /
avatar image
1
Question by klee57 · Jan 15, 2019 at 10:59 PM · objectcolor

Why is my object loading as black instead of the saved color?

I'm writing a script to save and load a color that the user picks for the 3D object (could be a cube, cylinder, or sphere) via button presses, but when I try loading the object in, it's just black. I've double checked and the right RGB values are being saved and loaded, but the object just appears to be black. Also, for now, I'm just testing the loading output with a default color, which is red in my case. Why is this happening and how can I fix it?

Important Note: I made a "private Renderer rend;" at the top of the class, and then defined rend ("rend =...") in the void Start() section.

         // Changing the color via key presses
 
         if (Input.GetKeyDown(KeyCode.R))
         {
             rend.material.SetColor("_Color", Color.red);
         }
         if (Input.GetKeyDown(KeyCode.G))
         {
             rend.material.SetColor("_Color", Color.green);
         }
         if (Input.GetKeyDown(KeyCode.B))
         {
             rend.material.SetColor("_Color", Color.blue);
         }

        // To add button elements to the visual interface
        void OnGUI() 
        {
            // Saving
            if (GUI.Button(new Rect(700, 330, 50, 30), "Save"))
            {
                 // Saving the object's color 
                 Color colorOfObject = rend.material.GetColor("_Color");
                 PlayerPrefs.SetFloat("rValue", colorOfObject.r);
                 PlayerPrefs.SetFloat("gValue", colorOfObject.g);
                 PlayerPrefs.SetFloat("bValue", colorOfObject.b);
         }
 
         // Loading
         if (GUI.Button(new Rect(770, 330, 50, 30), "Load"))
         {
             Color defaultColor = Color.red;
             Color colorOfObject = new Color(PlayerPrefs.GetFloat("rValue", defaultColor.r), 
             PlayerPrefs.GetFloat("gValue", defaultColor.g), PlayerPrefs.GetFloat("bValue", defaultColor.b));
             rend.material.SetColor("_Color", colorOfObject);
         }


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 sean244 · Jan 16, 2019 at 12:09 AM

Your code works fine for me. I'm able to switch it between, red, green and blue, and I'm also able to save and load it as green.

alt text


colors.gif (357.1 kB)
Comment
Add comment · Show 9 · 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 klee57 · Jan 16, 2019 at 12:17 AM 0
Share

Huh that's strange...:( Just a quick question, but does it matter what color I choose for the default?? I think I'm confused on how it's saving the user's chosen color if it's loading the default color. And this is what I see on my screen when I do the same process: alt text

screen-shot-2019-01-15-at-42311-pm.png (476.1 kB)
avatar image sean244 klee57 · Jan 16, 2019 at 12:33 AM 0
Share

I honestly don't know, but I'm testing it on a 2d gameObject with a spriterenderer - whose default color is white - for what that's worth.

avatar image klee57 sean244 · Jan 16, 2019 at 12:36 AM 0
Share

No worries, thanks for helping test it out!! I really appreciate it! I'm still a little confused on the logic of loading it though; isn't the new rendered material/color just the default RGB values?? I'm not sure how it's loading the color chosen by the user if the values are just being set to the default ones :O

Show more comments

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

105 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

How can I make objects change color, then fade/return to its original color? 1 Answer

change color object 2 Answers

Changing two different objects renderer colour 1 Answer

how to change to another object after button is pressed 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