- Home /
Other
Set prefab to public variable of another script
Hi there
I'm trying to set a prefab from the project folder to a secondary scripts' public variable. This is within an editor script.
Scenario is that the first script is creating a gameobject based on certain parameters. Once the user is happy with everything, they can press 'save', which creates the gameobject into a prefab. I now want this prefab to be stored as a public variable onto another script (which is already attached to a gameobject in the hierarchy).
I can find the second script through calling something like:
MyScript scriptInstance = FindObjectOfType( typeof(MyScript ) ) as MyScript ;
From within the first script, i'd like to then call something like
scriptInstance.m_prefabReference = m_myPrefabIjustMade;
Any ideas? Cheers!
I don't get your question...
When you have your prefab reference "m_myPrefabIjust$$anonymous$$ade" and a reference to the script instance that contains the variable, isn't the line you've posted exactly what you want?
scriptInstance.m_prefabReference = m_myPrefabIjust$$anonymous$$ade;
I don't get the problem...
Answer by hathol · May 22, 2012 at 11:59 AM
First thing that comes to mind is to pass the prefab path as a string to your second script and then use http://unity3d.com/support/documentation/ScriptReference/Resources.Load.html to get the actual prefab in there. Though there might be more efficient ways to do it.
I already have the path to the prefab since the first script is generating and saving the path using the AssetDatabase. I cannot use Resources.Load since i just need to get a reference to the prefab. (I'm needing to do this for an object pool).
All i basically would like to do is reenact the process of dragging a prefab from the project folder straight onto a script that has a public variable or array of variables ready to accept prefabs. Thanks for the suggestion though.
Follow this Question
Related Questions
Public variables set with custom editor buttons reset to prefab values on play? 2 Answers
Why does my prefab create a child by itself in ProjectView 0 Answers
How to keep reference linked when replace prefab through editor scripts? 1 Answer
Reference to Scene Object Component in Prefab Editor, 'Type Mismatch'? 1 Answer