- Home /
Cannot assign the Player to the Camera automatically
Goodday!
I am trying to modify MainCamera's script, so it finds the Player and assign it to itself. By default you just drag-drop the Player's gameObject into a MainCamera reference slot via Inspector, but I want camera to find the right gameObject [by Tag] and assign it upon the new Scene load.
And this code I have there does not work... or any variations I already went through... Ouch. I am stuck.
public GameObject player;
player = GameObject.FindWithTag("Player");
If the object with Tag Player exist when Awake() is run, it will work. You should get rid of the CameraTargets List if you only have one target, as is indicated by the code.
Thank you. Unfortunately this original script is huge and has gazillion references to the list. I try to modify it just the smallest amount, as I am not so versatile in scripting yet. If I comment out the List, the script brakes down by turning spectacularly red :) And, unfortunately, the support for this [not so cheap, $$anonymous$$d you] asset is very dismissal and close to non-existent.
Answer by UnderShad · Sep 13, 2020 at 01:28 PM
your method of finding object why findgameobjectwithtag is not ever used due to massive performance issue. but I will tell you that find game objects will return game objects not the cameraTarget script. Use
Gameobject[] playerArray=GameObject.FindgameObjectsWithTag("Player");
for(int i=0; i<playerArray.Length,i++) {
if(playerArray[i].getComponent<CameraTarget>())
CameraTargets.Add(playerArray[i].getComponent<CameraTarget>()); }
I will try it right away.
About performance issues: what would be the better method? By name?
UPD: Nope. It does not like that. The third line throws out "Cannot convert type to bool"
for (int i = 0; i < playerArray.Length; i++)
{
if (playerArray[i].GetComponent<CameraTarget>())
CameraTargets.Add(playerArray[i].GetComponent<CameraTarget>());
}
try
if (playerArray[i].GetComponent<CameraTarget>()!=null)
It does not throw errors in VS, but I am getting this upon launch:
ArgumentException: GetComponent requires that the requested component 'CameraTarget' derives from $$anonymous$$onoBehaviour or Component or is an interface.
Answer by ransomink · Sep 13, 2020 at 03:30 PM
Using FindWithTag
is okay is used in Awake or Start. Just best to avoid in Update, methods called multiples times per frame or often. FindWithTag will return a GameObject
type but your list is a CameraTarget
type. You have to check if the player game object returned has a CameraTarget
component/script on it
public void Start()
{
// Find and return the player GameObject
GameObject player = GameObject.FindWithTag("Player");
// Assign a reference to the CameraTarget component on the player GameObject
// If a CameraTarget component does not exist on the player, this will be null
CameraTarget target = player.GetComponent<CameraTarget>();
// Check if the target has a value (not null)
// If so, add target to the CameraTarget list
if (target) CameraTargets.Add(target);
}
But Player Object does not such a component. Never had. You just dragged the gameobject into the slot [within component on $$anonymous$$ainCamera] in the Inspector and lived happily ever after.
In the script you posted, you tried to add the player to a CameraTarget list. That suggests the player game object has a CameraTarget component attached and you wanted to access it. I had no idea you were using a paid asset to do this. You need to look at how they add to that camera target area in code and do the same. I can use that information to help you set it properly.
Saw you mentioned this once I clicked the "Show $$anonymous$$ore Comments" button to reveal your other conversations. Try this:
public void Start()
{
// Find and return the player GameObject
GameObject player = GameObject.FindWithTag("Player");
// Check if the player has a value (not null)
// If so, add player to the camera targets
if (player) ProCamera2D.Instance.AddCameraTarget(player.transform);
}