Hello, I have the following script attached to 5 different objects. Idea is to raycast, and if hit update counter and reflect it in Gui. Different values for different objects.
var particle : GameObject;
var hit : RaycastHit;
var valorHit:int = 10;
function Update () {
if (Input.GetButtonDown ("Fire1")) {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit)) {
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
clone=Instantiate (particle, hit.point, hitRotation);
Destroy(clone.gameObject, 5); clone.transform.parent = hit.transform;
var puntosGuiRef: puntosGui = GetComponent(puntosGui);
puntosGuiRef.MyScoreCounter +=valorHit;
}
}
}
My Gui Counter (puntosGuiRef) updates, but every object (valorHit)is sending the same value (even though I changed it in the inspector).
Put “var hit” inside “Update()” function, and change “var valorHit” to “static var valorHit”, and make a variable for clone, which would be a GameObject, so var clone : GameObject; inside your function or if statement. Try copying and pasting my edit of your code to see if this works. You may not need the static function, so try with and without. It’s easier for me to post code than explain, I really hope this works for you.
static var valorHit : int = 10;
function Update ()
{
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetButtonDown ("Fire1"))
{
if (Physics.Raycast (ray, hit))
{
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
var clone : GameObject;
clone=Instantiate (particle, hit.point, hitRotation);
clone.transform.parent = hit.transform; Destroy(clone.gameObject, 5);
var puntosGuiRef: puntosGui = GetComponent(puntosGui);
puntosGuiRef.MyScoreCounter +=valorHit;
}
}