- Home /
Camera Transform on Scene load C#
Hi All,
Got a question, I have the DoNotDestroy on load working and I've attached Char to a SpawnPoint to sort out Vector's on load all is working fine. The only issue I have is the camera Script uses a Transform to attach to the Player and I can't find away to attach it back automatically on Application Load Level, the Scene Asset ( and Transform point) doesn't exist until the scene is loaded.
If I do a DoNotDestroy with tag MainCamera, things get weird and I can't seem to tell the Camera Script to attach to the target automatically because it flags an error attaching the Transform to a GameObject Asset. Can anyone help?
Answer by $$anonymous$$ · Sep 17, 2013 at 11:53 PM
Well I managed to find the answer and this is so frustrating to say I missed one bit:
target = GameObject.Find("enterobjecthere").transform;
If you miss off the .transform at the end it throw's a wobbly.
Answer by Yokimato · Sep 17, 2013 at 08:45 PM
I usually have a "scene controlling script" that acts as the controller for whatever scene is loaded. If you have this controller, you can attach the main camera to your carried-over Character during the Start function of the controller.
Something to the effect of:
public void GameController: monobehaviour {
public void Start() {
var character = GameObject.Find("Player");
Camera.mainCamera.transform.parent = character.transform;
}
}
Really appreciate the assistance, I tried that script and a I tried one similar that I made: Both come up with the error:
Assets/LevelTrigger.cs(19,37): error CS0029: Cannot implicitly convert type UnityEngine.GameObject' to
UnityEngine.Transform'
I just updated it, you need to call character.transform
to assign it.
It breaks the camera LookAt, if I could find a way to statically assign a scene value to Public Transform target;. Then I'm sure it would work fine as the scene would load with the target in place. Again thanks for the help.