need to assign the Main Camera back to the parent Player

pulled out the camera using: GameObject.Find("Main Camera").transform.parent = null;

enter image description here

  • GameObject.Find ("Main Camera"). Transform.SetParent (GameObject.Find ("Player")); - Xumera_hZ
  • @Xumera_hZ, error CS1502: The best overloaded method match for 'UnityEngine.Transform.SetParent(UnityEngine.Transform)' has some invalid arguments - Kill Noise
  • GameObject.Find("Player") highlighted in red - Kill Noise
  • one
    Well, add .transform - Xumera_hZ

1 answer 1

If you use your own code, then like this:

 GameObject.Find("Main Camera").transform.SetParent(GameObject.Find("Player").transform); 

But GameObject.Find itself is advisable to use as rarely as possible. It is better to give the script direct links, or at least cache (i.e., assign the found file to the variable, so that you don’t look for it again through Find). those. something like this will come out:

 var _camera = GameObject.Find("Main Camera"); var _player = GameObject.Find("Player"); _camera.transform.SetParet(null); _camera.transform.SetParet(_player.transform); 
  • and how can I directly contact without using Find ? - Kill Noise
  • @KillNoise, well, for example, declare a variable, make it public, or add the attribute [SerializeField]. When you select an object on which this script is hanging, a field will appear there, where you can drag the object you need. You can then access this variable in the script. For example, you need to work with the transform of the camera and the transform of the player. Create two public variables Transform. Select an object with a script and drag the necessary objects there from the scene. This is perhaps the simplest option. - M. Green
  • understand everything, thanks) your code is incorrectly written SetParet you missed n you need SetParent - Kill Noise
  • @KillNoise, well, by simply copying other people's solutions, you cannot achieve anything - you need to search for yourself, figure it out, and try) and missed the letter - because not copied, but just typed in the editor here) is not very convenient. If my answer helped to solve the question - mark it as correct - then the applicants will immediately see that it is solved and where to look. - M. Green