- Home /
[Solved] Reading same data from multiple Scripts
I want to know if my Player or the Enemy is in Range of a Lightsource. Right now I'm using Raycasts and Tags to determine whether the enemy/player is in range of the Lightsource or behind a Wall. I attached the script to all Lightsources and tested it, right now there aren't any errors. The Problem is, I dont know how to read the Variables pLight and eLight. I wanted to make a 2nd script to get the Variables pLight and eLight from every Lightsource script in the scene (all Lights are tagged as Light) and determine whether one (or more) of them returns "true" (I dont need to know how many Lightsources are in range)
I would really appreciate any suggestions since I'm pretty new to Unity
raycasting code:
var pLight : boolean;
var eLight : boolean;
private var player : GameObject;
private var enemy : GameObject;
private var raydirection: Vector3;
private var raydirection2: Vector3;
private var hit : RaycastHit;
private var hit2 : RaycastHit;
private var hittag : String;
private var hittag2 : String;
private var MainCam : Camera;
private var MainCam2 : Camera;
function Update()
{
if (this.light.enabled == true){
Debug.DrawLine(this.transform.position, hit.point);
Debug.DrawLine(this.transform.position, hit2.point);
}
}
function Start()
{
MainCam = Camera.main;
MainCam2 = GameObject.Find("Camera").camera;
InvokeRepeating("FindPlayer", 1, 1);
InvokeRepeating("FindEnemy", 1, 2);
player = GameObject.FindGameObjectWithTag("Player");
enemy = GameObject.FindGameObjectWithTag("Enemy");
}
function FindPlayer()
{
hittag = null;
raydirection = player.transform.position - this.transform.position;
if (this.light.enabled == true){
if (Physics.Raycast (this.transform.position, raydirection, hit, this.light.range))
{
hittag = hit.collider.tag;
if ( hittag == "Player" )
{
print("in range");
pLight = true;
} else { print("out of range"); pLight = false;}
} else {
print("out of range");
pLight = false;
}
} else {
print("out of range");
pLight = false;
}
}
function FindEnemy()
{
hittag2 = null;
raydirection2 = enemy.transform.position - this.transform.position;
if (Physics.Raycast (this.transform.position, raydirection2, hit2, this.light.range))
{
hittag2 = hit2.collider.tag;
if ( hittag2 == "Enemy" ){
print("enemy in range");
eLight = true;
} else { eLight = false; print("enemy out of range");}
}
else
{
eLight = false;
print("enemy out of range");
}
}
Answer by Spinnernicholas · Nov 27, 2013 at 05:16 PM
This would allow you to access the raycasters from anywhere.
Add a static variable to your script that is a list of all of raycasting objects.
public static List<raycastingClass> instances;
And then you could have the class automatically add itself and remove itself from the list.
void Awake()
{
raycastingClass.instances.add(this);
}
void OnDisable()
{
raycastingClass.instances.remove(this);
}
Then you can access all the raycasters from anywhere.
foreach(raycastingClass raycaster in raycastingClass.instances)
{
//do something......
}
Thanks for the help, I searched for some list/array ScriptReference to learn a bit about lists and finally ended up using javascript-arrays In the "Collector-Script"
public static var pLight = new Array ();
And in the raycasting-Script
function Export(){
if ( pLight == true) {
playerDamage.pLight.Push("pLightTrue");
} else {
playerDamage.pLight.Push("pLightFalse");
}
if ( eLight == true) {
playerDamage.eLight.Push("eLightTrue");
} else {
playerDamage.eLight.Push("eLightFalse");
}
}
That will work if all you want to do is basically count how many lights. But you should just use numbers ins$$anonymous$$d of strings.
public static var pLight = 0;
and then
function Export()
{
if(pLight)
{
playerDamage.pLight++;
}
if(eLight)
{
playerDamage.eLight++;
{
}
if you want to track how many don't fit also:
public static var pLight = 0;
public static var pLightFalse = 0;
function Export()
{
if(pLight)
{
playerDamage.pLight++;
}
else
{
playerDamage.pLightFalse++;
}
if(eLight)
{
playerDamage.eLight++;
}
else
{
playerDamage.eLightFalse++;
}
}
I actually didn't think about using a number ins$$anonymous$$d of arrays >.< So the easiest way to solve this is just adding 1 to the playerDamage pLight/eLight if the Player/Enemy is in range of the Light. After that i can check if pLight and eLight is >0, perform the action and reset it to 0 Pretty nice, this way its also easy to check how many Lights the Player is in Didn't actually think about such an easy way , thanks
Yeah, now were talking! $$anonymous$$eep at coding, it will become more natural the more you do it.
Answer by chrisMary · Nov 27, 2013 at 05:40 PM
in an external script :
var lights : GameObject[];
var pLights : int = 0;
var eLights : int = 0;
function Start(){
lights = GameObject.FindWithTag("Light");
}
function Update(){
pLights = 0;
eLights = 0;
for(light:GameObject in lights){
if(light.GetComponent("yourscript").eLight){
eLights++;
}
if(light.GetComponent("yourscript").pLight){
pLights++;
}
}
// your logic here...
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Have to press Fire1 button twice to run if statement? 1 Answer
Raycast length or camera to between mousePosition and character 1 Answer
Getting a custom class to contain a certain script. 1 Answer
Using different paricle emitters depending on the tag of the object raycast hits? 1 Answer