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 AT-Brackeys · Mar 17, 2013 at 12:50 PM · arrayvariablelightinstance

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! :)

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

1 Reply

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

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!

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 AT-Brackeys · Mar 17, 2013 at 03:18 PM 0
Share

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 :)

avatar image Fattie · Mar 17, 2013 at 03:43 PM 0
Share

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)

avatar image AT-Brackeys · Mar 17, 2013 at 08:09 PM 0
Share

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 :)

avatar image Fattie · Mar 17, 2013 at 09:02 PM 0
Share

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 :)

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

11 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

Related Questions

Variable Type for an Instance of any Script 2 Answers

Change variable of ex2D Sprite using the component ExScreenPosition via code .js script 0 Answers

Storing many array values in a variable 1 Answer

Enumerations Varibles 1 Answer

How to get all instances of an object in the scen 2 Answers


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