- Home /
Adding up instances of a variable across different objects?
So I'm trying to create a stealth game where the player is easier to be detected by the AI if he stands in the light. Therefore I have come up with a point system for measuring light based on light intensity, range, distance to the player and objects in the way. The script has a variable called "theDistance" which gets higher and higher the more light is on the player. The script looks like this and is applied to all lights in my scene:
#pragma strict
var thePlayersHead : Transform;
private var theDistance : float;
function Start () {
thePlayersHead = GameObject.FindGameObjectWithTag("Head").transform;
}
function Update () {
transform.LookAt(thePlayersHead);
Debug.DrawLine(transform.position, thePlayersHead.position, Color.red);
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit))
{
if (hit.collider.gameObject.tag == "Head" && hit.distance < light.range)
{
//Debug.Log("Working");
theDistance = (light.range/hit.distance-1)*light.intensity;
}
else
{
theDistance = 0;
}
}
}
This part is working great. I have trouble with adding up all of the lights though. On the player I want a script that has a variable called "lightOnPlayer" that = the sum of all "theDistance" variables from the different lights across the scene. I tried sending it to the Players script usind Sendmessage though this wouldn't allow me to separate the different versions so the result was a constantly rising number because it kept adding the same variables over and over. I then thought of using arrays though I can't seem to get that to work either. I hope you have a good idea, thank you in advance! :)
Answer by Fattie · Mar 17, 2013 at 03:00 PM
really, I would just do this. is it true you have a FIXED, KNOWN number of lights in the scene?
on a script attached TO YOUR HERO: let's say the script is called HeroInfo.js for clatity.
var lightA:Light
var lightB:Light
etc
etc
function giveMeTheLightValueNow():float
{
var result:float;
result = 0.0;
result += your calculation lightA;
result += your calculation lightB;
result += your calculation lightC;
etc
return result;
}
then, your AI (or whatever) can just call that at any time.
your AI would already have a variable pointing to HeroInfo.js
private var heroFacts: HeroInfo;
...
heroFacts = GameObject.Find( .. your hero ..).GetComponent<HeroInfo>;
...
theLightingFactor = heroFacts.giveMeTheLightValueNow();
If your lights are not just a fixed known set, just loop through them in some way in the function giveMeTheLightValueNow
(Really your lights should be "kept together" in an array or something, if you have some extremely novel situation where lights are being created and destroyed constantly or whatever. Anyway that's irrelevant - in most situations just make a variable for each light as in the code fragment at the top.)
So it's that simple!
Thank you for your answer! I have thought about doing it the first way but is there any way to do it using an array ins$$anonymous$$d of a whole lot of variables? I don't know how to achieve this effect, arrays can be confusing :)
what's the particular reason you want to use an array?
Please tell how many lights you have exactly, cheers
if for some reason you wanted to use an array, don't use an array, use List
import System.Collections.Generic;
private var example:List.;
example:List = new List.();
each of your lights .. like this
example.Add( .. one of your lights .. )
this does seem totally pointless though, as you will have to "get at" each of your lights, exactly like I show in the answer by using a variable for each one: in the editor, drag each light on-to that variable spot (I'm sure you knwo about that procedure already)
Thank you so much for your help, I will try out the first method you described! :) The only reason why I wanted to use a list or an array was that I wanted the script to be usable without changing the code at all because I need to have people with very little JavaScript knowledge deploy it in their games :)
I see, in that case perhaps use the "List" method outlined, and search for all the lights. $$anonymous$$gest you search on here for 100s of questions on how to use List, also unityGE$$anonymous$$S.com for many beginner articles. Hope it helps! Looking forward to your next question :)