- Home /
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);
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>();
}
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
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.
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.
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.
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
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.
Your answer
Follow this Question
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