Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jamesslumburger · May 12, 2016 at 11:31 AM · javascriptarraylighttagfindgameobjectswithtag

How do I use arrays, with getcomponent?

this is my script, im sure its simple, i just want all the objects with the tag Light to have their light component disabled once the player steps into the box collider. but nothing happens when i step into the collider. i checked the tags are all right

var light2 : GameObject[];

function Start () { light2 = gameObject.FindGameObjectsWithTag ("Light");

}

function OnTriggerEnter (other : Collider){

if (other.gameObject.tag == "Player"){

for (var light : GameObject in light2){

light.GetComponent(Light).enabled = false; } } }

please help, i have been at this for too long trying to find out how to fix it.

Comment
Add comment · Show 2
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 tanoshimi · May 12, 2016 at 12:08 PM 0
Share

Your question title says you "keep getting errors". Your question description says "nothing happens". These are vary different problems, and when asking for help you really need to be specific. What errors are you getting?

avatar image jamesslumburger tanoshimi · May 12, 2016 at 01:41 PM 0
Share

Sorry about that I was getting an error, but then I fixed the error so there was no error, I will fix the title

2 Replies

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

Answer by HarshadK · May 12, 2016 at 11:39 AM

The method FindGameObjectsWithTag(string tag) is a static method.

You need to change your

 light2 = gameObject.FindGameObjectsWithTag ("Light");

to:

 light2 = GameObject.FindGameObjectsWithTag ("Light");

so that you are using the GameObject class to call the method and not instance i.e. gameObject.

Edit #1:

Just tested out and seems like it is okay in Javascript to use an instance to call a static method. Unity returns the proper array of gameobjects.

It is only C# that will throw an error when trying to access static method with an instance.

Comment
Add comment · Show 8 · 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 tanoshimi · May 12, 2016 at 12:07 PM 0
Share

Good spot. Surely this should have thrown an error in the original code?

avatar image HarshadK tanoshimi · May 12, 2016 at 12:28 PM 0
Share

@tanoshimi Just tested out and seems like it is okay in Javascript to use an instance to call a static method. Unity returns the proper array of gameobjects.

It is only C# that will throw an error when trying to access static method with an instance.

So there is actually something else wrong with the code that is causing the issue for OP.

avatar image tanoshimi HarshadK · May 12, 2016 at 12:50 PM 0
Share

Eurgh. Another reason to dislike Javascript!

Show more comments
avatar image jamesslumburger · May 12, 2016 at 01:15 PM 0
Share

So my script works, for like transfor$$anonymous$$g the game objects, but I think the problem is the getcomponent part, did I do this right?

avatar image HarshadK jamesslumburger · May 12, 2016 at 01:24 PM 1
Share

You are accessing the components correctly.

Just wrote this little code which works fine. It disables all light components in my scene when mouse button is clicked.

 #pragma strict
 
 var light2 : GameObject[];
 
 function Start () {
     light2 = gameObject.FindGameObjectsWithTag ("Light");
     Debug.Log(light2.length);
 }
 
 function Update()
 {
     if(Input.Get$$anonymous$$ouseButtonDown(0))
     {
         for (var light : GameObject in light2){
             light.GetComponent(Light).enabled = false;
         }
     }
 }

So it is something related to your collision code itself. You need to check for issue there.

avatar image Tyche10 jamesslumburger · May 12, 2016 at 01:25 PM 0
Share

Deleted my previous comment because I was wrong, sorry. Checking for null is never a bad idea when you try to get a component from a gameobject.

avatar image jamesslumburger · May 12, 2016 at 01:39 PM 0
Share

O$$anonymous$$ thanks so much that helps alot!

avatar image
0

Answer by UsmanAbbasi · May 12, 2016 at 11:35 AM

  • Check if "OnTriggerEnter()" is being called by using "Debug.Log()" or debugger.

  • If yes then check if Player gameobject has been assigned the tag "Player".

  • Finally check the length of light array. Is it filled correctly or not.

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

6 People are following this question.

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

Related Questions

Turn on/off renderer for objects found with "FindGameObjectsWithTag" 1 Answer

how to check when MoveTowards has finished 2 Answers

need help in randomising array 1 Answer

I cant figure out why my array is causing the error 'System.Type' does not support slicing. 0 Answers

how to not repeat random array 0 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