- Home /
Add camera prefab to script in c#
I would like to add a camera prefab to my script, I tried this, am I close?
public Transform PlanetCamPrefab = Instantiate(Resources.Load("PlanetCam"));
If I leave it empty, I have to manually add the prefab, how would you let c# do it automatically.
what about making an empty gameobject with the script attached, manually adding the prefab, and then saving the new object as prefab? :p
I don't get what you mean, I already created an empty game object and added this script, and the camera object I saved as a prefab. I am trying to avoid manually adding the prefab. 2 times already I forgot to add the cam and wondering why it wasn't working. That is why im trying to avoid adding it manually, less prone to errors.
never$$anonymous$$d, I hadn't thought that through. But couldn't you simply set the prefab PlanetCam as a component type, and then use [RequireComponent (typeof (PlanetCam))] ?
Answer by Tomer-Barkan · Oct 23, 2013 at 06:10 AM
You can try one of these options:
public GameObject PlanetCamPrefab = Resources.Load("PlanetCam") as GameObject;
Then you can use PlanetCamPrefab.transform to fetch the transform, or any other component. If you want to implicitly make it a transform from the start, use this:
public Transform PlanetCamPrefab = Resources.Load("PlanetCam", typeof(Transform)) as Transform;
Last, if you want to reference the Camera
component, then you need the variable to be of type Camera
as well:
public Camera PlanetCamPrefab = Resources.Load("PlanetCam", typeof(Camera)) as Camera;
For others having the same issues; After following this step, it didn't work. What I did to make it work is I added a folder named "Resources" and drop the PlanetCam inside of it. The folder can be placed anywhere in your asset folder.
And you can have as many Resource folders as you like!
Oh, and you can also have folders within folders. So within the Resources folder you can have a Prefabs folder, then you would load the resource with Resources.Load("Prefabs/my_prefab_name");
Answer by Sisso · Oct 22, 2013 at 09:54 PM
You cant use GameObject.Instantiate or Resources.Load in field initialization. Your best options is Awake, awake works the same as constructor (or field initialization), but it only runs when unity is ready to receive calls.
The usual method is:
void Awake() {
if (cam == null) cam = Resources.Load("Cam", Camera);
}
If you really need it, I think that the unique option is to create and EditorScript and resolve the reference before you run the app.
no, in the beginning where you initialize everything. I want to turn this;
public Transform PlanetCamPrefab;
into where it already has the planetCam attached to it.
I tried this
public Transform PlanetCamPrefab = Resources.Load("PlanetCam", Camera);
and this
public Transform PlanetCamPrefab;
void Start () {
if (PlanetCamPrefab == null) PlanetCamPrefab = Resources.Load("PlanetCam", Camera);
}
Both have the same error - Expression denotes a type where a variable, value or method group was expected.
PlanetCam is a prefab of a camera.
Have you tried Resources.Load("PlanetCam", typeof(Camera))?
Take a double check in examples http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html
The examples was the first place I been, no luck, I also tried
public Transform PlanetCamPrefab = Resources.Load("PlanetCam", typeof(Camera));
it says cannot convert object to transform
Your answer
![](https://koobas.hobune.stream/wayback/20220613120448im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Instantiate cube on network 0 Answers
Multiple Cars not working 1 Answer
Make Prefab Look At Target 2 Answers
GameObject transform relative camera 0 Answers