Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 iron_attorney · May 17, 2015 at 08:16 PM · layersfindfind-gameobject

Find Object By Layer (Unity 5, Split Screen Camera Follow)

Hi there! I am having issues getting my camera's to both follow 1 respawnable player object each. Both player objects are tagged as "Player" and they each have there own layer, layers 8 and 9. So far, I've got the cameras to work one at a time. It starts with player 1 camera following player 1 and player 2 camera still, then when player 1 dies, player 2 camera imidiately locks on and follows player 2, and player 1 camera stays still, and they keep swapping every time the tracked player dies. Crazy!

Here's my code anyway:

 public class CameraFollow : MonoBehaviour {

     int findlayer;

     Transform player;

     void Start ()
     {
         // Create a border around the camera view
 //        GUI.Box (Rect (cam.pixelRect.x, (Screen.height - cam.pixelRect.yMax), cam.pixelWidth, cam.pixelHeight), "");

         // If camera for player  01
         if (gameObject.tag == "MainCamera")
             findlayer = 08;
         else
         // If camera for player 02
         if (gameObject.tag == "MainCamera02")
             findlayer = 09;
         else
         // If camera for player 03
         if (gameObject.tag == "MainCamera03")
             findlayer = 10;
         else
         // If camera for player 02
         if (gameObject.tag == "MainCamera04")
             findlayer = 11;
     }
 
     // Update is called once per frame
     void Update ()
     {
         // If the player isn't currently being tracked
         if (player == null)
         {

             // Find players ship and save it as GameObject gO [aka. game Object]
             GameObject gO = GameObject.FindWithTag ("Player");

             // If the player exists and has the right layer, save it's position to the Transform "player"
             if(gO != null && gO.layer == findlayer)
             {
                 player = gO.transform;
             }
             else
                 return;
         }

         if (player == null)
             return;

         // By this point, we've either found the playe or he/she doesn't exist right now
     
         // HERE -- we know for sure we have a player, turn to face it 

         Vector3 targPos = player.position;
         targPos.z = transform.position.z;
         transform.position = targPos;
     }
 }

The problem is in the exert bellow. I think what is happening is that it is finding the first object tagged "player", and if it doesn't have the right layer, it just keeps checking that same player and therefor doesn't move. What I think might solve this issue, is to find the object by layer before checking the tag... I think? Or to find the object by both layer and tag at the same time? But I'm struggling to figure that out how to search for objects by layer.

 // If the player isn't currently being tracked
 if (player == null)
 {

     // Find players ship and save it as GameObject gO [aka. game Object]
             GameObject gO = GameObject.FindWithTag ("Player");

     // If the player exists and has the right layer, save it's position to the Transform "player"
     if(gO != null && gO.layer == findlayer)
     {
         player = gO.transform;
     }
     else
         return;
 }

Cheers in advance for your help and advice!

Pete

Comment
Add comment · Show 5
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 iron_attorney · May 17, 2015 at 02:04 PM 0
Share

I just realised that it's finding the objects in order in the hierachy, so both cameras just continually find player01 and stop there, until player01 dies and respawns, in which case player02 is highest on the list... I need to make it keep going down the list somehow.

avatar image iron_attorney · May 20, 2015 at 03:59 PM 0
Share

ok, I'm not going to do the search thing anymore, I've got a different idea involving attaching the camera to the player and preserving the camera between spawns...

but I am still interested to know how to search the entire hierachy for an object with a certain tag/layer ins$$anonymous$$d of stopping after the first one? Is there an indexing system that can be accessed for the hierachy or something so I can search the entire list until I've found the object I'm actually after?

avatar image iron_attorney · May 21, 2015 at 06:20 AM 0
Share

Yes! that's exactly the kind of thing I'm after!

I'm very new to Unity, and there's alot of code in what you typed there that i'd not seen before :D

Got 1 error come up though, it dosn't like your first line...

 GameObject[] gos = GameObject.FindObjectOfType(typeof(GameObject)) as GameObject[];

Throws up the error:

Assets/Scripts/CameraFollow.cs(39,92): error CS0039: Cannot convert type UnityEngine.Object' to UnityEngine.GameObject[]' via a built-in conversion

As for the logic behind my code, I'm gonna play the newbie card again ;)

Cheers for your help buddy!

Pete

avatar image barbe63 · May 21, 2015 at 03:30 PM 1
Share

Oh sorry my mistake, it should be:

 GameObject[] gos = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];

Dumb me I forgot a s

avatar image iron_attorney · May 23, 2015 at 01:32 PM 0
Share

Yes! That's done the job perfectly! Cheers very much buddy :D

Pete

2 Replies

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

Answer by barbe63 · May 20, 2015 at 06:41 PM

I don't understand your logic behind your code but you can do this:

 GameObject[] gos = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[]; //will return an array of all GameObjects in the scene
 foreach(GameObject go in gos)
 {
     if(go.layer=="LayerName" && go.CompareTag("Tagname")
     {
         //do something
     }
 } 

Hope it helps.

Just don't do it often as in an update loop ;)

Edited: I forgot a s in the syntax on FindObjectsOfType

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
0

Answer by JamesWolfgang · Jan 10, 2020 at 10:20 AM

I got an error with Barbe63 code. Here is mine :

 using UnityEngine;
 
 public class FindLyaer : MonoBehaviour
 {
     GameObject[] gos ; //will return an array of all GameObjects in the scene
     private void Start()
     {
         GameObject[] gos = FindObjectsOfType(typeof(GameObject)) as GameObject[];
         foreach (GameObject go in gos)
         {
             if(go.layer==9)
             {
                 Debug.Log(go.name);
             }
             Debug.Log("Done");
         }
 
     } 
 }
 
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 ZWLSOFTWARE · Oct 08, 2020 at 10:08 AM 0
Share

the problem is this:

 GameObject[] gos;
 private void Start()
 {
    GameObject[] gos = FindObjectsOfType(typeof(GameObject)) as GameObject[];
 }

you should do it like this:

 GameObject[] gos;
 private void Start()
 {
      gos = FindObjectsOfType(typeof(GameObject)) as GameObject[];
 }






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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I find my inactive objects and set them active again? 3 Answers

How to reference a GameObject that shares position with gameObject 0 Answers

How to find layer instead of tags 3 Answers

Searching a List of GameObjects by name 2 Answers

Finding GameObject without using the full name 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