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
0
Question by Crys · Jun 05, 2011 at 01:08 PM · editorcolormaterialsfontinstance

Pressing play Causes Re-instancing of material (Editor)

Ok. So I have a font asset and have created several text objects with the font asset in a scene. I then needed to change the colour of each text object to different, unique colours via the editor, So I created a script to do so and put it in "editor" folder to add the necessary Inspector objects (with @ExecuteInEditMode in the primary attached script). I did this because the colour of the font can only ever be one colour at a time-and it's greyed out in the inspector, and I needed to change the colour for each text object.

How do I prevent the instanced material (with it's new, picked colour) from being constantly replicated/instantiated each time it hits an OnInspectorGUI() event an GuiText is selected and PLAY is started? The font material is shown in the console as, "font material (Instance) (Instance) (Instance) (Instance) (Instance)" etc.

I only need the one original instance, so that the colour can be changed and preserved per text object. Not continued instances of instances! Apparently reading the material from the game object instantiates it every time or every time the thing is played... (sharedMaterial is obviously pointless because it's a read-only shared material across all objects using that particular font asset). Do I destroy the material? How? When I restart Unity it seems to preserve the instances...

Do I for example, need to set mat to 0/null/Destroy() it when the play is stopped (if so, what is the event called)? The colour of the text objects must persist after save scene events, though.

--Ok, should I be using GUITexts? Is UnityGUI- GUISkins labels look the same? (Apparently you can't easily change sizes with labels..)

---edit:

If it helps, this is the Inspector script part for the colour picker:

     target.guiText.material.color = EditorGUILayout.ColorField( "font colour:",

target.guiText.material.color );

I'm looking for ANY kind of Answer, even:

  • Use XYZ instead, it's better.

  • It's broke, try....

Anyone know anything at all about what the best course of action to take or what's going on, please post below.

Comment
Add comment · Show 3
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 Crys · Jun 06, 2011 at 11:55 AM 0
Share

Surely someone knows a solution? If it's a trivial issue to point me to a better text option- just add a single line comment.

avatar image Statement · Jun 06, 2011 at 12:49 PM 1
Share

Could you provide a $$anonymous$$imal package with the necessary scripts to reproduce this? I did some fiddling of my own (just in a regular Update) and I didn't get any duplicates.

avatar image Crys · Jun 06, 2011 at 01:31 PM 0
Share

Thankyou for trying...I am new to Unity, so I'm not sure how. But I see it happen with this code. Steps to create scene:

  1. Import a ttf font (Set to ASCII default -> Apply)

  2. Create 3 GuiTexts. Rename to (eg. "L1","L2", and "L3"). Change text via Inspector to same (ie. "L1","L2", and "L3")

  3. Put following code in "ChangeColour.js". Attach script to the 3 GUITexts. Change each 1s colour in Inspector (eg. select green, red, and a yellow).

    @ExecuteInEdit$$anonymous$$ode

    function Start() { Debug.Log( "\\""+guiText.name+"\\"" "+guiText.material ); }

    function Update () { }

  4. Put following code in "ChangeColourEditor.js" in the 'Editor' folder.

    @CustomEditor( ChangeColour )

    class ChangeColourEditor extends Editor {

    function OnInspectorGUI() { target.guiText.material.color = EditorGUILayout.ColorField( "font colour:", target.guiText.material.color ); } }

What happens:

When you stop and start PLAY repeatedly with one of the selected GuiTexts selected in the scene list, as you can see from the Debug log, Unity makes an instance of an instance of the material. When you select another guitext, Unity starts doing it with that 1. You can reset and start from scratch by re-selecting the font material in the GuiText component panel, then changing the colours again. It will start from one (instance) of the material (you can see the changes in the debug log.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by flaminghairball · Jun 07, 2011 at 01:53 PM

IIRC, making an assignment with '.material' will indeed make a new instance, which means that your script will instantiate a new material every time OnInspectorGUI is called.

Is there a reason you need to do this programmatically? You're going to have to use different materials for each different colored object anyway, so I'm not sure why you want to set this up via script.

As to the current problem, what you'll probably want to do is grab a reference to the material as soon as the object is selected, and just make changes to that.

Post back if you need any more help.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Alter Meshes in Editor by Script without warning 2 Answers

2 Instances of Unity 3 Answers

Destroying the instance of a gameObject on Android vs in Editor 0 Answers

Color custom editor window ? 2 Answers

check if 'B' key is being pressed while clicking in Editor window 3 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