- Home /
Layermask exception
I have this Radar code part from a project i downloaded from here:
By it self it's working fine. I'm taking the Test scene from the project to my Hierarchy and it's working fine. What it does is when i move my self around walking around when i'm getting close to cubes there are 5 cubes the cubes in the radar are in red points like enemies.
But i want to make something else now. In my own scene i did i have 5 cubes i added the Radar script to one of the cubes this cube rotate none stop by script i did. So now the radar is on the cube and rotating. What i want to do is when i'm getting close to the cube(radar) it will show on the radar me my self in red point like enemy moving close to the radar !
The first screenshot show it how it looks like with the porject i downloaded:
You can see on the right the game window the cubes and i'm from first person view moving close to the cubes they are in red points in the radar.
On the this screenshot now it's my scene you can see the cube the one in the middle is the one that rotate and the radar script is attached to it. There is a big red ball rotating with the cube part of the radar this ball in front of me is the border of the radar radius when it rotate.
The red ball behind not sure what it is.
Now i want to do when i move close to the rotating cube show me the character( ThirdPersonController ) in red point in the radar on the top right corner the green radar. Like now i'm the enemy ! Now the radar is static on the cube and when i will get close to it it will show in the radar( Radar Camera ) that i'm the enemy. Not like before the cubes.
This is the exceptions i'm getting two exceptions same exceptions errors on two lines:
A game object can only be in one layer. The layer needs to be in the range [0...31] UnityEngine.GameObject:set_layer(Int32) Radar:Update() (at Assets/Radar/Radar.cs:26)
And
A game object can only be in one layer. The layer needs to be in the range [0...31] UnityEngine.GameObject:set_layer(Int32) Radar:Update() (at Assets/Radar/Radar.cs:27)
This is the two lines 26 and 27:
borderObjects[i].layer = LayerMask.NameToLayer("Radar");
radarObjects[i].layer = LayerMask.NameToLayer("Invisible");
Not sure if this exceptions is the only thing that make my idea not to work.
This is the full Radar script :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Radar : MonoBehaviour {
public GameObject[] trackedObjects;
List<GameObject> radarObjects;
public GameObject radarPrefab;
List<GameObject> borderObjects;
public float switchDistance;
public Transform helpTransform;
// Use this for initialization
void Start () {
createRadarObjects();
}
// Update is called once per frame
void Update () {
for(int i = 0; i < radarObjects.Count; i++) {
if(Vector3.Distance(radarObjects[i].transform.position, transform.position) > switchDistance) {
//switch to borderObjects
helpTransform.LookAt(radarObjects[i].transform);
borderObjects[i].transform.position = transform.position + switchDistance*helpTransform.forward;
borderObjects[i].layer = LayerMask.NameToLayer("Radar");
radarObjects[i].layer = LayerMask.NameToLayer("Invisible");
} else {
//switch back to radarObjects
borderObjects[i].layer = LayerMask.NameToLayer("Invisible");
radarObjects[i].layer = LayerMask.NameToLayer("Radar");
}
}
}
void createRadarObjects() {
radarObjects = new List<GameObject>();
borderObjects = new List<GameObject>();
foreach(GameObject o in trackedObjects) {
GameObject k = Instantiate(radarPrefab, o.transform.position, Quaternion.identity) as GameObject;
radarObjects.Add(k);
GameObject j = Instantiate(radarPrefab, o.transform.position, Quaternion.identity) as GameObject;
borderObjects.Add(j);
}
}
}
Did you make sure that Layer$$anonymous$$ask.NameToLayer("Radar") returns an integer between 0 and 31? Do you have a Layer "Radar" in the Tags and Layers settings? You can use Debug.Log(Layer$$anonymous$$ask.NameToLayer("Radar"));
In the original scene of the downloaded project Test i clicked on every object and checked in the Inspector of each object there is no tags or layers for any of them. All of them Untagged without a layer. $$anonymous$$aybe i didn't check right. I also click on Layers > Edit Layers and there is no Radar layer. And no Radar tag. so i wonder if in the original test there is no any tag/layer why and how should i add in my scene ?
For example when i click on the Radar Camera i see untagged and layer default. If i click on Radar i see untagged and layer is empty.
The objects in the scene don't need to have layers assigned to them because that's done through code in those two lines 26 and 27 you posted. But layers with those names do need to exist in the project.
I added to my scene a two Layer name invisible and radar and also a tag name radar. Now i'm not getting errors/exceptions but i still don't see the character in the radar when getting close to the rotating cube.
Did you add your player object to the tracked object list in the inspector?
Your answer
Follow this Question
Related Questions
Why it;s never getting to the OnMouseDown function when clicking with the mouse ? 1 Answer
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers