Unity 3D – Changing color on game object changes color on other objects in scene

Problem: When changing the shader color on a game object, the color on other game objects changes.

Reason: This happens because the other game objects are using the same Mesh Renderer as the game object your modifying.  So when your changing the shader, your actually modifying the Mesh Renderer, not the game object.

Fix: Change the Mesh Renderer for the game object.

In the example picture below, when you change the color on the cube (highlighted red) the color on the capsule changes, because they both are using the same Mesh Renderer (highlighted green).  To fix, change either the Cube or Capsule to a different Mesh Renderer (highlighted green)

Unity 2D C# Script to Spawn Object on Screen at Touch Position

You’ll need a touch screen to test this out.  Easiest way is to use the Unity Remote 5 app.  It allows you to use your Android device as a touch screen input.

Start by creating a new empty C# script named “TouchScript” and paste in the following code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchScript : MonoBehaviour {

public GameObject bullet;

void Start () {

}

void Update () {
Touch myTouch = Input.GetTouch (0);
for(int i =0; i < Input.touchCount; i++)
if (myTouch.phase == TouchPhase.Began) {
{
Debug.Log ("Touch Position" + myTouch.position);
SpawnBullet (myTouch);
}
}
}

void SpawnBullet(Touch fireTouch) {
Vector3 touchPos = Camera.main.ScreenToWorldPoint (fireTouch.position);
touchPos.z = 1; // Puts the z coordinates at 1 so it is visible to the camera.
Debug.Log("Vector3 Pos" + touchPos);
Instantiate (bullet, touchPos, Quaternion.identity);
}
}

Now you’ll need to put your script on a game object.  You should be able to drag and drop it on any game object, in this case the Main Camera “green”.

Next drag and drop the game object prefab to the script “red”.  This tells the script which game object to spawn or instantiate when you touch the screen.

Run the scene.  It should now create a game object where you touch on the screen.