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 /
avatar image
0
Question by Chocolade · Dec 16, 2016 at 09:01 AM · c#scripting problemscript.

Layermask exception

I have this Radar code part from a project i downloaded from here:

Radar Project Link

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:

Original

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.

My Scene

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);
         }
     }
 }
 


ss20.jpg (393.7 kB)
ss22.jpg (376.7 kB)
Comment
Add comment · Show 7
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 UnityCoach · Dec 16, 2016 at 09:10 AM 0
Share

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"));

avatar image Chocolade UnityCoach · Dec 16, 2016 at 11:03 AM 0
Share

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.

avatar image tanoshimi Chocolade · Dec 16, 2016 at 11:34 AM 0
Share

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.

Show more comments
avatar image Chocolade UnityCoach · Dec 16, 2016 at 11:10 AM 0
Share

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.

avatar image UnityCoach Chocolade · Dec 16, 2016 at 11:43 AM 0
Share

Did you add your player object to the tracked object list in the inspector?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

263 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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