- Home /
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
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?
Yes sorry - I want to activate all the objects in a certain layer once a player hits a collider.
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
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!
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;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613103630im_/https://answers.unity.com/themes/thub/images/avi.jpg)