Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by plusko · Nov 27, 2013 at 04:49 PM · raycastreading data

[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");
     }    
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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......
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image plusko · Nov 27, 2013 at 05:39 PM 0
Share

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");
     }
     }


avatar image Spinnernicholas · Nov 27, 2013 at 05:45 PM 0
Share

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++;
   }
 }
avatar image plusko · Nov 27, 2013 at 06:02 PM 0
Share

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

avatar image Spinnernicholas · Nov 27, 2013 at 06:10 PM 0
Share

Yeah, now were talking! $$anonymous$$eep at coding, it will become more natural the more you do it.

avatar image
0

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...
 
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges