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 TomatoOrgyLT · Mar 02, 2014 at 05:09 PM · gameobjectarraydisableenableprojector

How to disable an array of projectors

So I have difficulties in understanding how does all the array things work. I am trying to disable a bunch of projectors with the same tag whenever I click the mouse. The code that I used gives me this error: 'enabled' is not a member of 'UnityEngine.Projector[]'. Any help would be appreciated.

Here is the code

 private var allProj : Projector[];
 private var buildingProjections : GameObject[];
 
 function Start () {
 buildingProjections = GameObject.FindGameObjectsWithTag("BuildingProjections");
 
 for(var i : int = 0; i < buildingProjections.Length; i++)
 allProj[i] = buildingProjections[i].GetComponent(Projector);
 }
 
 function Update () {
         if (Input.GetButtonDown("Shoot"))
         {
         for(var go : Projector in allProj)
         {
         allProj.enabled = false;
         }
         }
 }

I tried changing this line:

         for(var go : Projector in allProj)
         {
         allProj.enabled = false;
         }

to this:

     for(var i : int = 0; i < allProj.Length; i++){
     allProj[i].enabled = false;
     }

but then it just gave me an error when I clicked the mouse.

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 fafase · Mar 02, 2014 at 08:29 PM

I think your problem comes from the fact you are not initializing your Projector array.

 private var allProj : Projector[];

this is just a pointer meant to be used with an array of projector but you still need to tell how big this should be.

 private var allProj : Projector[];
 private var buildingProjections : GameObject[];
  
 function Start () {
 // Here the array is created inside the method and the pointer is pointing at that array
 buildingProjections = GameObject.FindGameObjectsWithTag("BuildingProjections");
 
 // Here you need to tell how many spots your array should have:
 allProj = new Projector[buildingProjections.Length];
 for(var i : int = 0; i < buildingProjections.Length; i++)
    allProj[i] = buildingProjections[i].GetComponent(Projector);
 }
Comment
Add comment · Show 1 · 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 TomatoOrgyLT · Mar 03, 2014 at 03:39 PM 0
Share

Thank you very much! That solved my problem.

avatar image
0

Answer by MakeCodeNow · Mar 02, 2014 at 06:50 PM

The original problem is that it should have been written like this:

 for(var go : Projector in allProj)
 {
     go.enabled = false;
 }

It's also possible that your array has null entries in it, maybe that's the cause of the issue in the last block. In that case you can try this:

 for(var go : Projector in allProj)
 {
     if(go)
     {
         go.enabled = false;
     }
 }
Comment
Add comment · Show 1 · 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 TomatoOrgyLT · Mar 02, 2014 at 08:23 PM 0
Share

Thanks for correcting my code and it does look like my array has null entries. Something is wrong with this line of code

 for(var i : int = 0; i < buildingProjections.Length; i++) {
 allProj[i] = buildingProjections[i].GetComponent(Projector);
 }

because it can not assign any projectors. Any ideas what could be wrong?

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

21 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

Related Questions

Help With Disabling/Enabling single GameObject 1 Answer

When object i in Array is enabled, all other objects in Array are disabled. 3 Answers

Problem with gameObject Array 1 Answer

how to perform undo action in unity? 2 Answers

Enable/Disable Game Object With GUI Button 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