- Home /
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?
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];
}
}
}
}
}
}
When i reach my home i'll test, thanks for the piece of code. I'll feedback soon.
Cool, I'll take the time to write an actual script for you to test out, this is just an example.
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.
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
$$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.
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.
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.
$$anonymous$$ake sense, my head was hot, I get hit by this detail
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
When you want to edit an INSTANCE of a material, you can do wonders lol.
No worries man, just want to help everyone, including you.
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.
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.
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?
if(Physics.Raycast(ray.origin, ray.direction * 10, out hit) && hit.transform == touchTarget){
touchTarget.renderer.material.color = Color.white;
Debug.Log("Touch Touch!");
}
this.
You should set touchtarget inside the raycast.
if(Physics.Raycast(ray, out hit))
{
touchTarget = hit.collider.gameObject;
touchTarget.renderer.material.color = (Your Color);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613111727im_/https://answers.unity.com/themes/thub/images/avi.jpg)