- Home /
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
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.
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?
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
Oh sorry my mistake, it should be:
GameObject[] gos = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];
Dumb me I forgot a s
Yes! That's done the job perfectly! Cheers very much buddy :D
Pete
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
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");
}
}
}
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[];
}