- Home /
Create Transform from prefab file in a project
How can load a prefab for example from "Assets\Tower Defense Pack\Prefabs\Turret\Turret.prefab" into a:
public Transform building;
using C# script, but not a drug and drop method through the inspector? thx)
Answer by Borgo · Sep 22, 2011 at 07:55 PM
You need to put your files in a folder called "Resources" and use:
public Transform building;
void Start(){
building = Resources.Load("File") as Transform;
}
Don't include the file extension.
Answer by jonas.du · Sep 22, 2011 at 08:09 PM
If you want to load a resouce dynamicly you must place it in your 'resources' directory and load it via 'Resource.Load()', please refer to the documentation about resources.
Example:
var instance : GameObject = Instantiate(Resources.Load("enemy"));
If you have your gameObject in any way (instantiated, drag and drop via inspector) you can access the transform of the gameObject with the 'transform' property.
Example:
public GameObject myGameObject; // drag'dropped via inspector or load it via resources
public Transform building;
building = myGameObject.transform;
Your answer
Follow this Question
Related Questions
rot not working for 2nd prefab 2 Answers
Assign a 'Transform' target to prefab by finding a 'GameObject' 1 Answer
is that possible to load prefab from script. 1 Answer
Setting Position of Spawned Prefab 2 Answers
How to load a script at runtime? 0 Answers