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 elpulpobastardo · Dec 10, 2017 at 07:45 PM · listsinstantiate prefabmaterial colormaterial rendererexecuteineditmode

ExecuteInEditMode doesn't work on instantiated list of objects?

I have a prefab of a single tile, and for level design purposes I want to be able to make it 'invalid' and change it's colour to red both in play and edit modes. It has a bool checkbox in the inspector which allows me to do this.

I have made sure to include [ExecuteInEditMode] at the top of this script, and the below code WORKS JUST FINE in both play mode and edit mode.

 [SerializeField]
 private bool isBlocked;    

     private Renderer rend;                
 
     void Awake(){
         rend = gameObject.GetComponent<Renderer> ();
     }
     
     void Update(){
         DisplayTileValidity ();
     }
 
     void DisplayTileValidity(){
         if (isBlocked || isOccupied) {
             rend.sharedMaterial.color = Color.red;                        
         } else {
             rend.sharedMaterial.color = Color.white;
         }
     }


These tiles are prefabbed, and used as a gameobject to be instantiated in a grid... The code I am using is here, and WORKS FINE except for that I now cannot change the colour of the tile using that checkbox. It won't work in edit mode, or play mode.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GridGenerator : MonoBehaviour {
 
     [Header("Grid Size")]
     [SerializeField]
     private int rowsAmount = 3;                            //The width of the level
 
     [SerializeField]
     private int columnsAmount = 3;                        //The length of the level
 
     [Header("Parents and Prefabs")]
     [SerializeField]
     private GameObject tilePrefab;                        //The tile prefab gameobject.
 
     [SerializeField]
     private Transform tileParent;                        //The Tile Parent gameobject for storing our tiles nicely in the hierarchy.
 
     private List<Vector3> gridPositions = new List<Vector3>();
 
     void Awake(){
         GenerateGrid ();
     }//end awake
 
     public void DefineGridPositions(List<Vector3> gridPositions){
 
         gridPositions.Clear ();                                        
 
         for (int x = 0; x < rowsAmount; x++) {                        
         for(int z = 0; z < columnsAmount; z++){                    
 
         Vector3 gridPosition = new Vector3 (x, 0, z);            
         gridPositions.Add (gridPosition);                    
 
             }//end for column
         }//end for row
 
     }//end define grid positions
 
     public void CreateGrid(List<Vector3> gridPositions){
 
         foreach(Vector3 gridPosition in gridPositions){
             GameObject tile = Instantiate (tilePrefab, gridPosition, Quaternion.identity) as GameObject;
             tile.transform.parent = tileParent;
         }//end foreach
 
     }//end create grid
 
     public void GenerateGrid(){
         DefineGridPositions (gridPositions);
         CreateGrid (gridPositions);
     }//end generate grid
 
 }


I assumed that by instantiating a whole list full of these tiles that I would simply be able to click whichever one I want in the hierarchy and make it red/ invalid, but that doesn't seem to be the case.

Do I have to add a script to the tile holder object or a function to the grid generator script to iterate over these tiles and change their material colour that way?

Please remember that the solution must work in both play and edit modes.

Thanks very much.

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
Best Answer

Answer by Bunny83 · Dec 10, 2017 at 09:29 PM

I'm sure it does work perfectly, however you use the sharedMaterial. So each tile uses the same material. So if you have multiple objects all with the same material and all with this script attached, only one of them (the last one that is executing) will have an effect on all objects. If you want to change material properties only for a single instance you should use .material instead of .sharedMaterial.


However keep in mind that this will break batching. If you only have two colors you want to toggle between you should simply create two seperate material and just exchange the sharedMaterial of the renderer you want to toggle.

Comment
Add comment · Show 1 · 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 elpulpobastardo · Dec 11, 2017 at 06:13 PM 0
Share

Sweet! Thanks! Glad it was a simple fix.

When using a single tile in the scene, Unity actually flashes up a warning if you use 'material.color' saying:

Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.shared$$anonymous$$aterial ins$$anonymous$$d. UnityEngine.Renderer:get_material()

...which was my reason for doing it in the first place. Any idea why Unity dislikes material.color for a single object? It works fine, but Unity just complains.

But yeah. Changing it to 'material.color' throws no warnings when you generate a grid of tile prefabs, and it works perfectly in both edit and play modes :)

In the interest of keeping batching intact I am going with your 2x materials option.

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

71 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

Related Questions

Emissive Material control via script (code) 1 Answer

How to pass color of two colliding gameobjects to each other? 2 Answers

Material cant be seen until Im very close 1 Answer

How to check if two objects share the same material within an if statement? 0 Answers

Set Specific Child's Material 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