- Home /
Static variable problem when using same script on multiple objects!
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.
Answer by Mike 3 · Dec 25, 2010 at 04:15 AM
Remove the word static, and it should work
By definition, static variables don't belong to a certain instance, it belongs to the Type itself (so all instances of the Type see the same variable)
It didn't work. If I remove the "static" from "static var remainActive : boolean = false;" of Change$$anonymous$$aterial.js, I get this error:
InvalidCastException: Cannot cast from source type to destination type. Shoot.Update () (at Assets/Scripts/Player/Shoot.js:35)
the line 35 of Shoot is this: var affectThis : GameObject = hit.collider/.gameObject/.GetComponent(VerifyRelatedJS).relatedObject;
What is wrong with those scripts? Any logic? what? thanks
O$$anonymous$$G! never$$anonymous$$d my previous comment, I was typing the wrong thing.
I did what you said, removing the "static" from that variable. I also did something that might have helped to fix the problem, which was changing the script VerifyRelatedJS.js "var relatedObject : Change$$anonymous$$aterial;" to "var relatedObject : GameObject;".
It worked like a charm! Thank you for explaining how static variables work and helping me solve this bug that was cursing me! And thanks for the excellent xmas gift! $$anonymous$$erry xmas everyone.
Your answer
Follow this Question
Related Questions
Accesing components on multiple objects in c#? 2 Answers
GetComponent for all enemies 1 Answer
Why is the MeshFilter being removed? 1 Answer
Unity3D Editor Glitch 0 Answers
Error in script 1 Answer