So I’ve a public variable for my main camera in a script
public var mainCamera : GameObject;
When I drag and drop my MainCamera onto the slot for this variable in the inspector on a prefab in the scene, it slots in fine and turns bold as you would expect. But, when I hit apply to apply it to the prefab it does not go unbold and does not update in the prefab in the project.
When I try to drag and drop the MainCamera onto the prefab’s variable slot for the main camera in the project, the slot in the inspector completely ignores it and doesn’t even highlight. Like it would do if say you tried to drop a GameObject onto a texture2d variable or something.
I’ve tried switching the variable type to camera but I get the exact same problems, and I’ve used this exact method on many other scripts in the scene already.
Can anyone shed any light on this for me before I dropkick this machine out the window?
As far as I know, Prefabs are designed to be drag-dropable into any scene. To this end, they will only maintain references to other GO’s that are part of the prefab chain.
If, for example, your hierarchy looks like this:
MainCamera
PrefabRoot
PrefabRoot/PrefabChild
And you have a script on PrefabRoot or PrefabChild - that script would be able to maintain references to either of those two game objects (since they are part of the prefab) - but not to MainCamera.
The reason being that there is no way to guarantee that the “MainCamera” object is available in every scene.
In this particular case, you can always access the MainCamera through:
Camera.main
Well the thing is I’ve referenced the main camera in the exact same way at least 20 times already in this project, but thanks for the Camera.main tip, that should do the trick!
Edit, nah it didn’t, I guess Camera.main is for camera component specific stuff, I’ll just search for and assign it in the start function.
Thanks for your reply tho I appreciate it
You should be able to call Camera.main.gameObject to get the gameObject that the main camera is attached to.
If you want an instantiated prefab to hook up to a game object in the scene, you have to have it find that object, or, rather the script. If CameraScript.cs is a component of the Main Camera it would look like this:
using UnityEngine;
using System.Collections;
public class MyScript : MonoBehaviour
{
CameraScript myCamScript;
void Start()
{
GameObject myCam = GameObject.Find("Main Camera");
myCamScript = myCam.GetComponent<CameraScript>();
}
void Update()
{
myCamScript.functionWhatever();
}
}
It’s actually not an instantiated prefab, it’s just some irritating Unity glitch, thanks for the reply tho :]
Then why are you trying to ‘apply’ the change to a prefab? Why not just let it sit in your scene?
Well what I mean is I am not trying to assign these variables at runtime, just in the inspector. The method I’m currently using is what you’ve suggested above here.
Thinking about it now, every other occasion where I’ve setup prefabs they have all been within a single hierarchy, as in all under one main parent, I think that’s what Kyle had said to me earlier and I miss understood. My brain was fried last night tho. Looks like I’d best go apologize to Unity and hope she takes me back hehe
Thanks for the help guys I appreciate it
mY BRain is frozen at this point. gotta sleep …
sleep
sleep