x


Create Transform from prefab file in a project

How can load a prefab for example from "AssetsTower Defense PackPrefabsTurretTurret.prefab" into a:

public Transform building;

using C# script, but not a drug and drop method through the inspector? thx)

more ▼

asked Sep 22 '11 at 07:22 PM

tfile101 gravatar image

tfile101
1 2 2 3

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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.

more ▼

answered Sep 22 '11 at 07:55 PM

Borgo gravatar image

Borgo
998 9 13 25

(comments are locked)
10|3000 characters needed characters left

thanks a lot, already done so:))

more ▼

answered Sep 23 '11 at 03:34 PM

tfile101 gravatar image

tfile101
1 2 2 3

(comments are locked)
10|3000 characters needed characters left

Thanks a lot, already done so:))

more ▼

answered Sep 23 '11 at 03:34 PM

tfile101 gravatar image

tfile101
1 2 2 3

(comments are locked)
10|3000 characters needed characters left

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;
more ▼

answered Sep 22 '11 at 08:09 PM

jonas gravatar image

jonas
91 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3340
x1281
x1260
x437
x242

asked: Sep 22 '11 at 07:22 PM

Seen: 638 times

Last Updated: Sep 23 '11 at 03:34 PM