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 laurienash · Jun 06, 2013 at 11:43 AM · collisionlayers

Script that activates all objects in layer on collision not working

Hello, I'm writing a script that activates all the objects in a layer once my player hits a collider.

This is the script I have so far, but I'm getting the error 'active' not a member of 'int' (JavaScript):

 var layer : int;
 
 
 function Start () {
 
 layer = gameObject.layer = 8;
 
 layer.active = false;
 
 }
 
 function OnTriggerEnter (other : Collider) {
 
 layer = gameObject.layer = 8;
 
 if(other.gameObject.tag == "Player") {
 
 gameObject.layer.active = true;
 
 }
 
 }

Any ideas on what I'm doing wrong (sorry for the newbie question!)

Thanks, Laurien

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 asafsitner · Jun 06, 2013 at 01:37 PM 0
Share

What exactly are you trying to accomplish?
What do you mean by 'activates layer'? Do you mean to activate all objects in a certain layer?

The error you are getting is in place - a layer is just a number.
What exactly are you trying to activate?

avatar image laurienash · Jun 06, 2013 at 02:03 PM 0
Share

Yes sorry - I want to activate all the objects in a certain layer once a player hits a collider.

2 Replies

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

Answer by Scribe · Jun 06, 2013 at 03:17 PM

Hello again!

Firstly, if this code was to work:

 layer = gameObject.layer = 8;
 layer.active = false;

you would never be able to reactivate the objects as the code is attached to an object that is deactivated, meaning it stops being called. So make sure you attach this code to an object not on layer 8

Here's some code that utilises the answer to this question

 var layer : int = 8;
 var LayerObjects : GameObject[];
 
 function Start () {
     LayerObjects = FindGameObjectsWithLayer(layer);
     ActivateGameObjects(LayerObjects, false);
 }
 
 function OnTriggerEnter (other : Collider) {
     if(other.gameObject.tag == "Player") {
         ActivateGameObjects(LayerObjects, true);
     }
 }
 
 function FindGameObjectsWithLayer (layer : int) : GameObject[] {
     var goArray = FindObjectsOfType(GameObject);
     var goList = new System.Collections.Generic.List.<GameObject>();
     for (var i = 0; i < goArray.Length; i++) {
         if (goArray[i].layer == layer) {
             goList.Add(goArray[i]);
         }
     }
     if (goList.Count == 0) {
         return null;
     }
     return goList.ToArray();
 }
 
 function ActivateGameObjects (Objects : GameObject[], activate : boolean){
     for(var go : GameObject in Objects){
         go.active = activate;
     }
 }

So calling FindGameObjectsWithLayer(someInt); will return an array of all objects on that layer which in the start function I save to 'LayerObjects' this is important as it is the only way to find the objects again once they are deactivated.

Then calling ActivateGameObjects(listofGameObjects, TrueorFalse); will set the activity of all objects in 'listofGameObjects' to 'TrueorFalse'

Make sure you dont call LayerObjects = FindGameObjectsWithLayer(layer); again whilst the objects are deactivated as this will show up as having no objects on layer 8

Scribe

Comment
Add comment · Show 2 · 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 laurienash · Jun 06, 2013 at 03:43 PM 0
Share

Wow that's so brilliant! Thanks so much! Yes I'd found that question too - I was trying to write it so it worked for me, but I kept getting different errors. (I'm going to compare $$anonymous$$e and yours now so I can see what I was doing wrong) Thanks again!

avatar image Scribe · Jun 06, 2013 at 04:29 PM 0
Share

no problem, glad you got it working!

avatar image
0

Answer by SubatomicHero · Jun 06, 2013 at 01:40 PM

Hi Laurien,

You will get that error as you're using the layer integer not the gameobject layer. All you're doing in start() is assign int layer = 8;

Change Start() to this:

 function Start() {
     gameObject.layer = 8;
     layer = gameObject.layer;
     gameObject.layer.active = 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 laurienash · Jun 06, 2013 at 02:04 PM 0
Share

That's still giving me the same error :(

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

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to check if an object is colliding with another from another script ? 1 Answer

Endless DeathZone??!!? 4 Answers

super mario pipe physics? 1 Answer

Script is stuck in OnCollisionStay 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