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 OldLadyNevermore · Aug 18, 2013 at 05:38 PM · editorprefab

How do I reference a prefab after I create it in editor?

I have a prefab called HexPrefab, it contains a Transform (of course), a C# script called HexScript, and a cube that just exists to make it visible in the Editor.

While in 'EditorTime' I am using this code to bring copies of this prefab into the scene:

private Transform hex;
Instantiate(hex, new Vector3(0,0,0), Quaternion.identity);

But when I do that I cannot (for example) access the script easily: it says (truthfully) Transforms to not have scripts.

Maybe I am just thinking like a java programmer, but what I would like to do is create a "Hex" object from that prefab, then be able to call the script for that object directly. The following seems to be what I want to do, but when I do this it says "Type or Namespace 'HexPrefab' could not be found.

private HexPrefab hex;
Instantiate(hex, new Vector3(0,0,0), Quaternion.identity);

What is the best way to (in 'EditorTime') create an object from a prefab, then (in Runtime) be able to access the functions and variables in its HexScript?

If I may make up some pseduocode it would look like:

HexPrefab hex = Instantiate(hex, new Vector3(0,0,0), Quaternion.identity);

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

3 Replies

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

Answer by robertbu · Aug 18, 2013 at 05:54 PM

A game object has multiple components. Some are your scripts, others are things such as Transforms and Rigidbodies. Generally you use GetComponent() to get any component on a rigidbody. Unity provides you shortcuts for common components such as Transforms, Audio Sources and Rrigidbody. So to keep things simple, here is what you can do:

 public GameObject prefab; // Initialized in the inspector
 
 void Start() {
 
    GameObject go = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
 
    Transform theTransform = go.transform;
    Rigidbody theRigidbody = go.rigidbody;
    Hex hex = go.GetComponent<Hex>();
 
 }
Comment
Add comment · Show 4 · 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 OldLadyNevermore · Aug 18, 2013 at 06:39 PM 0
Share

In this line:

Hex hex = go.GetComponent();

How does it know what a Hex (capital H) is? I have not defined a Hex class. Should that say HexPrefab

avatar image robertbu · Aug 18, 2013 at 08:30 PM 0
Share

I assumed from your script above that you had a Hex script. This line refers to a script named 'Hex' that is attached as a component to the prefab. You can replace it with whatever script you have attached to the prefab.

avatar image OldLadyNevermore · Aug 18, 2013 at 09:00 PM 0
Share

I see that now: the following appears to work.

HexScript hex = go.GetComponent();

I am still working on this, but it APPEARS to me that I pretty much have to treat my hex as a GameObject, and call GetComponent to get the script. There is no way to get to where I can say "hex.getName()", I need to use "hex.GetComponent().getName()" (or some similar thing) ins$$anonymous$$d.

avatar image robertbu · Aug 19, 2013 at 04:52 AM 0
Share

You don't need GetComponent() if you are dealing with a game object. 'name' is a property of a game object, so you can access it directly:

 if (go.name == "Player")

You only need GetComponent() when you want to access a component that does not have a shortcut. See the GameObject reference page for all the things that can be access without a GetComponent() call. Note that some/most of these actually have a GetComponent() that is done for you and therefore not visible.

avatar image
0

Answer by cdrandin · Aug 18, 2013 at 05:51 PM

Here you go, I think this is what you want. http://answers.unity3d.com/questions/214085/find-prefab-path-of-a-gameobject.html

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
avatar image
0

Answer by weenchehawk · Aug 18, 2013 at 07:02 PM

Let's say you create a prefab (called Hex) with lots of stuff attached, and you want to use it in your script you have three options :

1) Provide an explicit public reference to it in your class (below). In this instance when you put the script into your scene you drag the prefab (from your scene) and drop it over the "My Hex" value of the script

 class DoSomethingWithMyHex : MonoBehaviour
 {
   Transform MyHex
 
   public void Start()
   {
     // make a call on my Hex Object
     MyHex.GetComponent<ScriptAttachedToMyHex>().FunctionInTheScriptIAmCalling();
   }
 }

2) Instantiate the Object explicitly (usually only used if you're creating the prefab in your scrpt). In this instance when you put the script into your scene you drag the prefab (from your folder where it is stored) and drop it over the "Hex Template" value of the script

 class HexFactory : MonoBehaviour
 {
   public Transform HexTemplate;
   public GameObject CreateANewHex()
   {
     Instantiate(HexTemplate, Vector3.zero, Quaternion.identity) as GameObject;
   }
 }

3) Instantiate the Object explicitly & Dynamically In this instance you must put the Prefab object in your Resources folder and instantiate it by name

 class Factory : MonoBehaviour
 {
   public GameObject CreateObject(string Name)
   {
     Instantiate(Resources.Load(Name), Vector3.zero, Quaternion.identity) as GameObject;
   }
 }

I get the impression I still haven't answered the question but hopefully we've progressed the discussion. Finally, I haven't tested any of the above code so it may choke on something as simple as syntax or things more subtle. Hopefully it gives enough of the answer to put you on the correct path.

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

Generating a prefab from Audio Clip via script 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can I mimic the "Apply" button in editor script? 1 Answer

Need help with editor (script?) 1 Answer

How can I preserve static object/data between editor and play? 2 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