Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by d112570 · Oct 22, 2013 at 09:48 PM · c#cameraprefab

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Miziziziz · Oct 22, 2013 at 11:42 PM 0
Share

what about making an empty gameobject with the script attached, manually adding the prefab, and then saving the new object as prefab? :p

avatar image d112570 · Oct 23, 2013 at 12:10 AM 0
Share

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.

avatar image Miziziziz · Oct 23, 2013 at 01:36 AM 0
Share

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))] ?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image d112570 · Oct 23, 2013 at 02:38 PM 0
Share

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.

avatar image flaviusxvii · Oct 23, 2013 at 02:56 PM 0
Share

And you can have as many Resource folders as you like!

avatar image Tomer-Barkan · Oct 23, 2013 at 02:58 PM 0
Share

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");

avatar image
0

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.

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image d112570 · Oct 22, 2013 at 10:03 PM 0
Share

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.

avatar image Sisso · Oct 22, 2013 at 10:09 PM 0
Share

I changed my answer, see if it helps

avatar image d112570 · Oct 22, 2013 at 11:16 PM 0
Share

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.

avatar image Sisso · Oct 23, 2013 at 12:15 AM 0
Share

Have you tried Resources.Load("PlanetCam", typeof(Camera))?

Take a double check in examples http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html

avatar image d112570 · Oct 23, 2013 at 01:02 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges