Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 janaoravcova · Jan 09, 2020 at 01:45 PM · materialprefabsprefab-instanceindependent

Changes done in prefab script get applied to all instances of that prefab.

I am making multiple instances of prefab that has a script asign to it. In the script I am simply changing its color on mousedown. But clicking on one instance of prefab in the scene makes all other instances to change. I tried to change the material, so that each instance has its own material, but it did not seem to help. By logging I found out, that the method called on mousedown gets call for each instance. Could anyone explain this behaviour to me? I attached the code of script of the prefab and also code that I instantiate the prefabs with.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class markTile : MonoBehaviour
 {
 
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
             {
                 Debug.Log(GetComponent<Renderer>().transform.position+"clicked prefab");
 
                 Color currentEditorColor = EditorController.getEditorController().getCurrentEditorColor();
                 GetComponent<Renderer>().material.color = currentEditorColor;
             }
         }
     }
 }


   public void CreateTileMap()
     {
         for(int i=0; i<gridWidth; i++)
         {
             for(int j=0; j<gridHeight; j++)
             {
                 GameObject tileObject = Instantiate(tileItem);
                 tileObject.transform.localScale += new Vector3(5,5,5);
                 tileObject.transform.position = new Vector3(i * tileOffset + 5 + defaultOffset, j*tileOffset + 5 + defaultOffset, 0);
 
                 //this is the instance I am having problem with
                 GameObject topRightTile = Instantiate(innerTileItem);
                 topRightTile.transform.position = new Vector3(i * tileOffset + 5 + defaultOffset, j * tileOffset + 5 + defaultOffset, 0);
 
                 Material material = new Material(Shader.Find("Specular"));
                 topRightTile.GetComponent<Renderer>().material = material;
            
             }
         }
     }

Comment
Add comment · Show 4
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 unity_-8WUXdN3quKrvw · Feb 23 at 11:42 PM 0
Share

Hi, I got the same problem, have you found the cause of it and/or a solution?

avatar image janaoravcova unity_-8WUXdN3quKrvw · Feb 24 at 03:50 PM 0
Share

Hi, I am not sure as it was quite a while ago. I can check the code and refresh my memory.

avatar image janaoravcova unity_-8WUXdN3quKrvw · Feb 24 at 04:06 PM 0
Share

I am not sure if it is going to be of any help for you, I am not able to find the previous versions of the code and I am not sure what innerTileItem referred to in the code above but this is how I made the instance in the later version:

avatar image janaoravcova unity_-8WUXdN3quKrvw · Feb 24 at 04:07 PM 0
Share
 public override GameObject CreateTile(int i, int j)
         {
             GameObject tileObject = Instantiate(Resources.Load<GameObject>("Prefabs/FirstLevelTile"));
             tileObject.transform.localScale += new Vector3(25,25,25);
             tileObject.transform.position = new Vector3(i * TileOffset, j*TileOffset, -10f);
         
             return tileObject;
         }

0 Replies

· Add your reply
  • Sort: 

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

139 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 avatar image avatar image avatar image avatar image avatar image avatar image 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

How do I blend terrain prefabs together? 0 Answers

Referencing the currrently edited prefab in prefab edit-mode 0 Answers

How to create/update Prefab in Runtime? 1 Answer

Instantiated Prefabs Are Destroyed After 4 instantiations 1 Answer

Change Animator Parameters on Different Object - Prefabs 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