- Home /
Instantiate Resources Load not working ...
I have done this a million times, and even last night i had a similar line that worked just fine...but this morning it does not and i cant figure out what has changed.
var t:Transform=Instantiate(Resources.Load("ValuedAssets/CoverManager"),Vector3.zero,Quaternion.identity)as Transform;
The path is correct, if i just use instantiate w/o the cast the object instantiates fine. The problem is once i cast t as Transform it becomes null. This should just be routine, i dont get errors anywhere else... any ideas?
Answer by Berenger · Jun 18, 2012 at 10:41 PM
Load returns an object, not a transform. Try
(Wrong one)
var t : Transform = Instantiate(Resources.Load("ValuedAssets/CoverManager"),
Vector3.zero,Quaternion.identity).transform;
(Good one)
var go : GameObject = Instantiate(Resources.Load("ValuedAssets/CoverManager"),
Vector3.zero,Quaternion.identity);
var t : Transform = go.transform;
// OR
var t : Transform = (Instantiate(Resources.Load("ValuedAssets/CoverManager"),
Vector3.zero,Quaternion.identity) as GameObject).transform;
That gives me error: ' transform is not a member of UnityEngine.Object '. I seriously had a line that worked perfect last night, and this morning does not...
Thanks, for some reason casting as Transform wont work? It worked last night somehow, but this morning did not... Seems strange that the cast to GameObject would work but cast to Transform doesnt. Anyway works now, thanks.
Your answer
Follow this Question
Related Questions
Know when prefab has finished loading after Resources.Load 2 Answers
Save instantiated objects, to load later 2 Answers
How to, tell apart, Instance or Instantiated GameObject vs Loaded GameObject. 0 Answers
How to tell which enemys to create in new battle scene 1 Answer
Why this error when trying to instantiate object ?(Solved) 2 Answers