It works great when I have just 1 gameObject in the scene, but not when I have more than one.
After pressing the mouse, a raycast will be cast and hit an object tagged "RemoveCollision". The object has a script "VerifyRelatedJS.js" which holds another gameObject, which has a "ChangeMaterial.js" script with a static variable named "remainActive". I need to set the "remainActive" variable to true of just the gameObject inside the "VerifyRelatedJS.js" script (of the object that was hit).
The problem is that when I hit something, my project is changing the remainActive variable of all the gameObjects in the scene that have the "ChangeMaterial.js" script.
So: press mouse -> hit object -> remainActive = true (of ONLY the object in VerifyRelatedJS)
This is what I have:
Shoot.js - the script used to hit (is attached to Crosshair, which is not child of Player)
var range;
function Start ()
{
range = ChangeMaterial.powerDist + 2;
}
function Update ()
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
Debug.DrawRay (ray.origin, ray.direction * range, Color.green);
var facingDirection = transform.TransformDirection (Vector3 (0, 0, 1));
if (Input.GetButtonDown("Power"))
{
if (Physics.Raycast (ray, hit, range))
{
if (hit.collider.gameObject.CompareTag ("RemoveCollision"))
{
hit.collider.isTrigger = true;
// Get the object related to the one hit
var affectThis : GameObject = hit.collider.gameObject.GetComponent(VerifyRelatedJS).relatedObject;
// Now, change the var remainActive to true from its ChangeMaterial script
var obj : ChangeMaterial = affectThis.GetComponent(ChangeMaterial);
obj.remainActive = true;
}
else
{
Debug.Log ("Nothing hit yet." + " I hit: " + hit.collider.name);
}
}
}
}
VerifyRelatedJS.js - script added to the gameObject that is hit and is tagged "RemoveCollision"
var relatedObject : ChangeMaterial;
ChangeMaterial.js - holds the main code/ script added to the same object as VerifyRelatedJS' relatedObject
enum feedbackType {switchMaterial, switchColor, zFighting};
var whatToUse : feedbackType = feedbackType.switchMaterial;
var mat1 : Material;
var mat2 : Material;
var colorStart : Color;
var colorEnd : Color;
var zFightingObject : GameObject;
private var dist;
static var powerDist : float;
private var changeColor : boolean = false;
var player : GameObject;
var particleFeedback : Transform;
private var zFightIt : boolean = false;
// The static variable that is accessed from other scripts:
static var remainActive : boolean = false;
var remAct : boolean = false;
function Start()
{
powerDist = 7.5;
player = GameObject.FindWithTag("Player");
if (whatToUse == feedbackType.zFighting)
{
zFightingObject.gameObject.active = false;
}
}
function Update ()
{
remAct = remainActive;
dist = Vector3.Distance(player.transform.position, transform.position);
if (dist <= powerDist)
{
// Change the Color of the interactive object
if (changeColor == false)
{
changeColor = true;
// Instantiate the feedback
var theParticleFeedback = Instantiate (particleFeedback, transform.position, transform.rotation);
Destroy (theParticleFeedback, 3);
}
/*if (whatToUse == feedbackType.switchColor)
{
renderer.material.color = Color.Lerp (colorStart, colorEnd, 4);
}
if (whatToUse == feedbackType.switchMaterial)
{
renderer.material = mat2;
}*/
if (whatToUse == feedbackType.zFighting)
{
if (zFightIt == false)
{
zFightIt = true;
zFightingObject.gameObject.active = true;
}
if (remainActive)
{
renderer.enabled = false;
}
}
}
else
{
/*if (whatToUse == feedbackType.switchColor)
{
renderer.material.color = colorStart;
changeColor = false;
}
if (whatToUse == feedbackType.switchMaterial)
{
renderer.material = mat1;
changeColor = false;
}*/
if (whatToUse == feedbackType.zFighting)
{
if (remainActive)
{
changeColor = true;
zFightIt = true;
zFightingObject.gameObject.active = true;
renderer.enabled = false;
}
else
{
changeColor = false;
zFightIt = false;
zFightingObject.gameObject.active = false;
}
}
}
}
I spent like 2 days reading unityAnswers but I had no success. I hope after posting my own question this will be fixed.