Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by itsBINXbro · Apr 10 at 01:53 PM · arraylooptroubleshooting

Array only detects if last entry in array has a true boolean

I've created a "Light Detection" script (attaches to light objects) where the goal is to shoot a raycast from the light object to the player if the player is within a certain radius. This all works fine.


My problem lies within the secondary "Stealth Management" script (attached to player). The ultimate goal is to check if just ONE light object detects the player, _isPlayerLit = true.


So in order to accomplish this, I figure the script should first create an array, determining how many light objects there are in a scene in the Start function. And then checks every lightObject for the bool value via Update function. If all bool values = false, _isPlayerLit != true.


Simple enough, right?


Well, I'm entirely new to programming, and this is my first run-in with an array. I've spent the past few days trying to understand, but no matter what, the function I create only sets _isPlayerLit to true based on the last light object in the array.


I know it's something stupid-simple, but I just can't seem to find the solution for my problem.


This is my first post on the forum, and I don't really know the exact information you Unity experts need to determine my issue, so I will just give you both of my scripts.

Here's the Light Detection script, which does its job:


 {
     // lightObject radius variables
     public bool playerInRange;
     public float lightRange;
     Light lightObject;
     GameObject player;
 
     // lightObject raycast variables
     public bool isPlayerLit;
     Vector3 origin;
     Vector3 posPlayer;
 
     void Awake()
     {
         // lightObject gathering info about its variables
         player = GameObject.FindGameObjectWithTag("Player");
         lightObject = GetComponent<Light>();
         lightRange = lightObject.range;
     }
 
     void Update()
     {
         // Checks if player is within range of lightObject
         if (Vector3.Distance(lightObject.transform.position, player.transform.position) < lightRange)
         {
             // If player is in range of lightObject, playerInRange = true & a raycast is cast from the lightObject to the player
             playerInRange = true;
 
             RaycastHit hit;
 
             origin = lightObject.transform.position;
             posPlayer = player.transform.position;
             Debug.DrawLine(origin, posPlayer, Color.red);
 
             // If raycast = true
             if (Physics.Linecast(origin, posPlayer, out hit))
             {
                 // If the raycast has no obstructions to the player, isPlayerLit = true
                 if (hit.collider.gameObject.tag == "Player")
                 {
                     isPlayerLit = true;
                 } else
                 {
                     isPlayerLit = false;
                 }
             }
         } else
         {
             // If the player is not in range of lightObject, playerInRange is false, and isPlayerLit can't be true.
             playerInRange = false;
             isPlayerLit = false;
         }
     }
 }



And here's the Stealth Manager script, where the problem lies:


 {
     public bool _isPlayerLit;
 
     private GameObject[] lightList;
 
     void Start()
     {
         lightList = GameObject.FindGameObjectsWithTag("StealthLight");
     }
 
     private void Update()
     {
         for (int i = 0; i < lightList.Length; i++)
         {
             if (lightList[i].GetComponent<OldLightDetection>().isPlayerLit != true)
             {
                 _isPlayerLit = false;
             }
             else
             {
                 _isPlayerLit = true;
             }
         }
     }
 }

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

0 Replies

· Add your reply
  • Sort: 

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

193 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 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 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 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 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 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 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 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 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 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 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

Which one to use for, while or something else? 1 Answer

if ALL items in array are something 1 Answer

Create a 2D array (3 columns, 1000 rows)? 1 Answer

,"For" loop looping only once through my array 0 Answers

Same for each loop with multiple arrays? 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