ScriptName temp = (ScriptName)Instantiate(Resources.Load("MyPrefab"));

Hi,

I was woundering if it is possible to do:

ScriptName temp = Instantiate(Resources.Load("MyPrefab")) as ScriptName;

MyPrefab has a a ScriptName script attached to it. If it is possible I'll change my code, but it's a lot I'll have to change, so I want to be sure before doing it :P

Edit:

I now have the following:

BasicTyle newTile = Instantiate(tile) as BasicTyle;

("tile" is an Object I load elsewhere using Resources.Load(...)) When I do this, it says:

Object reference not set to an instance of an object

Edit 2:

I fixed the problem, using this:

GameObject newTileGO = Instantiate(tile) as GameObject;
BasicTyle newTile = newTileGO.GetComponent("BasicTyle") as BasicTyle;

Thanks !

-The Oddler

1 Answer

1

See http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

It says:

Instantiate can also clone script instances directly. The entire game object hierarchy will be cloned and the cloned script instance will be returned.

So I think you'd forget about it being on a prefab and just name the script.

Ok, so I guess this is a yes :P :D I read that scriptreference thingy, yet didn't fully understand. After reading a few more times I now understand :D Thanks

hmm, doesn't seem to work, I updated my main post with more info.