Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Stormizin · Aug 07, 2013 at 07:26 PM · c#prefabobjectbehaviour

How to make two objects with the same directory source got different behaviours

Basically i'll explain this with a example:

I have a Dot A and a Dot B in my scene. Both of the dots are from the Dot prefab.

When i click in Dot A or Dot B the material rendering color change the color from Red to White.

The Question is: When i click whatever(Dot A or Dot B) the another Dot got the same event and do the same things, like a mirror, i want both with same script, but not sharing variables.

Any ideas?

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

3 Replies

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

Answer by clunk47 · Aug 07, 2013 at 07:46 PM

Simply check the tag of the touchTaget object you are clicking on. For this example, create an empty scene with 2 cubes. Don't worry about the material, the default diffuse will be fine. You will only be changing the color of an INSTANCE of the material, so each cube will have its own color, same material.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     Ray ray;
     RaycastHit hit;
     Color color1 = Color.green;
     Color color2 = Color.red;
     GameObject touchTarget;
     
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown(0))
         {
             touchTarget = hit.collider.gameObject;
         
             if(touchTarget != null)
             {
                 if(touchTarget.tag == "DotA")
                     hit.collider.renderer.material.color = color1;
                 if(touchTarget.tag == "DotB")
                     hit.collider.renderer.material.color = color2;
             }
         }
     }
 }
 



EDIT: If you are going to have multiple "Dot" gameObjects, I would use an array. The only painful part of this would be assigning 20 different colors in the inspector, the rest is all in this small script. For this example, place 20 (Twenty) Cubes in your scene, just tag them "Dot", all that same tag that we'll use in the script. This code will grab all of your objects tagged "Dot" and put them in an array. Then it will assign the color depending on the index they hold in the array. Hope this makes sense, give it a shot.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     Ray ray;
     RaycastHit hit;
     public Color[] colors = new Color[20];
     GameObject[] dots;
     GameObject touchTarget;
     
     void Awake()
     {
         dots = GameObject.FindGameObjectsWithTag ("Dot");    
     }
     
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown(0))
         {
             touchTarget = hit.collider.gameObject;
         
             if(touchTarget != null)
             {
                 if(touchTarget.tag == "Dot")
                 {
                     for(int i = 0; i < dots.Length; i++)
                     {
                         if(dots[i] == touchTarget)
                             dots[i].renderer.material.color = colors[i];
                     }
                 }
             }
         }
     }
 }
 
 
Comment
Add comment · Show 10 · 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 Stormizin · Aug 07, 2013 at 07:48 PM 2
Share

When i reach my home i'll test, thanks for the piece of code. I'll feedback soon.

avatar image clunk47 · Aug 07, 2013 at 08:00 PM 1
Share

Cool, I'll take the time to write an actual script for you to test out, this is just an example.

avatar image clunk47 · Aug 07, 2013 at 08:17 PM 1
Share

Let me know if you are even instantiating these prefabs. I may have misinterpreted your question. If you are simply wanting to click on them to change color let me know. I will have time to rewrite this shortly.

avatar image Stormizin · Aug 07, 2013 at 08:20 PM 1
Share

I'am thinking about to instantiate or put the objects in the scene already, not decided yet, i don't know what method is faster

avatar image Stormizin · Aug 07, 2013 at 10:25 PM 2
Share

$$anonymous$$y bad Dude this code works, the question from now is how to performance that code, cuz if i got 20 dots in a scene ill need to make 20 ifs? of course i can make a for to loop through then. I'll mark your answer as answered, any more question can i P$$anonymous$$ you? Thanks dude.

Show more comments
avatar image
0

Answer by Xtro · Aug 07, 2013 at 07:49 PM

It's not about script behavior is being mirrored. Materials are global objects. If you change the color of a material, it's gonna affect all the object that uses that material.

You should create two materials for two objects. Even if they are coming from same prefab, you can assign different materials in the scene objects.

Comment
Add comment · Show 7 · 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 clunk47 · Aug 07, 2013 at 07:56 PM 1
Share

Actually, you can have the same material say on 4 cubes. Then you can throw a script on each cube to change material.color, you can change each individual cube's color, even having all the same material.

avatar image Stormizin · Aug 07, 2013 at 07:59 PM 1
Share

$$anonymous$$ake sense, my head was hot, I get hit by this detail

avatar image clunk47 · Aug 07, 2013 at 08:40 PM 1
Share

Try it dude. Try my script that I posted. It does not affect any other objects. You should TEST things before you give incorrect information. Not to be rude or anything, just saying, I know what I'm talking about here because I test every single bit of code I post in any answer I give before I post it. And before you say "They're all the same color!", make sure you chose three different public colors in the inspector. Try my example :D

avatar image clunk47 · Aug 07, 2013 at 08:54 PM 1
Share

When you want to edit an INSTANCE of a material, you can do wonders lol.

avatar image clunk47 · Aug 07, 2013 at 08:58 PM 1
Share

No worries man, just want to help everyone, including you.

Show more comments
avatar image
0

Answer by Joyrider · Aug 07, 2013 at 08:00 PM

It all depends what the logic is for clicking on your dots. But if that is not the problem (and I guess it is not), just call renderer.material instead of renderer.sharedMaterial when you change the color of your material, and it will only affect that particular object's material.

If the object's material is used by other renderers, calling renderer.material will clone the material and always use that one instead, from that point on.

Comment
Add comment · Show 5 · 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 Stormizin · Aug 07, 2013 at 08:19 PM 0
Share

Actually, i'am using renderer.material.color = Color.white; It's sharing the color cuz its the same material. But i think i'll need the event physics hit for each Dot.

avatar image Joyrider · Aug 07, 2013 at 09:27 PM 0
Share

Hmm strange, I guess that every object changes his material in the same manner then... what do you use to detect the click on a dot?

avatar image Stormizin · Aug 07, 2013 at 09:59 PM 0
Share
 if(Physics.Raycast(ray.origin, ray.direction * 10, out hit) && hit.transform == touchTarget){
                 touchTarget.renderer.material.color = Color.white;
                  Debug.Log("Touch Touch!");
             }

this.

avatar image clunk47 · Aug 07, 2013 at 10:06 PM 1
Share

You should set touchtarget inside the raycast.

 if(Physics.Raycast(ray, out hit))
 {
     touchTarget = hit.collider.gameObject;
     touchTarget.renderer.material.color = (Your Color);
 }
avatar image clunk47 · Aug 07, 2013 at 10:06 PM 1
Share

Did you happen to take a look at my edited answer?

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

17 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

Related Questions

Distribute terrain in zones 3 Answers

How do I instantiate certain objects to appear in a specific spot? 3 Answers

How to destroy instantiated objects. 1 Answer

Multiple Cars not working 1 Answer

Deactivating one object trough script deletes all versions of it in scene 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