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 vitaly mind · Sep 23, 2013 at 06:56 AM · prefabvariablereferenceassigndrag and drop

How to make reference from prefab to variable via C#/JavaScript?

Dear Unity,

I was looking for a way to make reference from Prefab to variable of type GameObject.

Inside the editor, it is no problem at all. Just drag and drop a prefab on to public variable, of type GameObject, inside your GameObject`s script.

But, queastion is, how to perform this "drag and drop" via script?

Example:

We know name of prefab lets say "Soldier".

We have script like this. It will instantiate GameObject from variable ObjectTest.

 using UnityEngine;
 using System.Collections;
     
 public class TEST : MonoBehaviour 
 {
     public GameObject ObjectTest;
     
     void Start ()
     {
         Instantiate(ObjectTest);
     }
 }

This will work only if "Soldier" prefab is referenced to variable in editor by drug and drop it. just like in this official Unity tutorial http://www.youtube.com/watch?v=4rZAAHevq1s&list=PLX2vGYjWbI0RmYmP19OimzWSy2PpLxMk7∈dex=23

While I do understand, I can create an empty GameObject with reference to Prefab, and then use it each time instantiate needed. But still... I do want to know how to do it by script.

Thank you for reading and hopefully answering. Have a nice day!

Comment
Add comment · Show 2
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 syclamoth · Sep 23, 2013 at 06:58 AM 0
Share

This should contain everything you need.

Of course, that class only works in the editor. You can't create that kind of prefab reference in a build, because Unity relies on editor-assigned prefab references in order to deter$$anonymous$$e what prefabs have to be packaged! So if you try to reference a prefab that the build system doesn't know about, it won't work.

avatar image syclamoth · Sep 23, 2013 at 07:18 AM 0
Share

That is correct. If this were not the case, you could easily find yourself enormous builds filled with junk data, especially if you are keeping deprecated assets around.

1 Reply

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

Answer by vitaly mind · Sep 23, 2013 at 07:17 AM

Dear syclamoth,

Thank you for pointing me this simple thing.

So, drag and droping actually importing prefab from Assets to build, and thus prefab can be accessed from inside build it self. In other words all prefabs in Assets folder, do not go to build automatically, and thus they are not part of GameObjects collection and thus cant be found by GameObject.find(); method.

By syclamoth as an answer,

That is correct. If this were not the case, you could easily find yourself enormous builds filled with junk data, especially if you are keeping deprecated assets around.

Comment
Add comment · 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

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

16 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

Related Questions

How to use a float value from coroutine 1 and use in coroutine 2? 1 Answer

How do I assign a prefab reference to a private variable? 1 Answer

How to assign prefab at runtime? 1 Answer

Creating a Prefab and Keeping Original Objects Script Information 0 Answers

Error message "variable not assigned" ? 1 Answer


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