- Home /
Interact with objects from Resources.Load
How do I interact with objects loaded from the resources? I have an empty objects on which I have attached a box collider, a script that rotate the object with mouseclick and finally a script to load the real object taken from the resources:
#pragma strict
public var model:String;
function Start () {
var instance : GameObject = Instantiate(Resources.Load(model,GameObject),Vector3.zero,Quaternion.identity);
}
Everything works well when using internal objects, but when I load from resources it will only display the object without the possibility to interact. Is there another method to interact with objects taken from the resources?
Please help! Many thanks
What path are you using?
The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted. All asset names & paths in Unity use forward slashes, paths using backslashes will not work.
I don't think is a problem of the path because the object displays correctly, but I can't do anything with it :-(
How you are interacting with it? Could you specify what you are doing?
The above script is attached to an empty object then I have 2 more things attached in the inspector which are a box collider and a script to rotate the object with the mouse, that is it. As state, it works perfectly if I place the same obect in the scene but it does not work when loaded from resources
means you made same object prefab in Resources ? Or you missed some component in it(like BoxCollider , Script)
Answer by Bunny83 · Nov 07, 2014 at 01:34 PM
I guess your empty gameobject should be some kind of placeholder for the object you load from Resources. You might want to make it a child of your empty Gameobject once you instantiated it. Because at the moment your instantiated object is on it's own and has nothing to do with your empty gameobject:
// [...]
var instance : GameObject = Instantiate(Resources.Load(model,GameObject),Vector3.zero,Quaternion.identity);
// make it a child of this gameobject.
instance.transform.parent = transform;
// move the child to local 0,0,0 inside the empty gameobject
instance.transform.localPosition = Vector3.zero;
instance.transform.localEulerAngles = Vector3.zero;
// [...]
Hi Bunny83 Thanks a lot!!! that Solved the problem. Now I have this:
function On$$anonymous$$ouseDown(){
var instance : GameObject = Instantiate(Resources.Load(model,GameObject),Vector3.zero,Quaternion.identity);
// make it a child of this gameobject.
instance.transform.parent = transform;
// move the child to local 0,0,0 inside the empty gameobject
instance.transform.localPosition = Vector3.zero; instance.transform.localEulerAngles = Vector3.zero;
}
Dou you know how can I implement it so that everytime I click it will loads a new objects and it destroy the previuos one? Sorry but I am not much of a programmer, I can go a bit trough script but I can't really think in program$$anonymous$$g langauages.
@paganic: Sorry, but your question was about how to interact with an instantiated object. In your code you only have one model name string so without having an array or list of (possible) model names you won't get anywhere.
Anyways this is something that doesn't belong to this question. Ask a new question and provide more details on your specific problem there. If this question is solved, don't forget to tick an answer.
Answer by Chamkey · Nov 06, 2014 at 06:10 PM
I had a somewhat similar problem just 2 weeks ago. I'm assuming that the interactivity comes from a gameobject that you define. As in drag and drop from the hierarchy onto your script?
The problem is that when you instantiate this object the script doesn't know which object its behaviour is coming from.
The normal object I'm assuming is the child of the empty GO.
var model: String;
var emptyObject: GameObject;
function Start()
{
var thisObject: Gameobject.Instantiate(Resources.Load(model, GameObject), Vector3.zero, Quaternion.identity);
//please tag your empty with whatever you want. I'm just using Parent
emptyObject = GameObject.FindObjectWithTag("Parent")
thisObject.transform.parent = emptyObject.transform;
}
Answer by imagoculturae · Nov 06, 2014 at 07:30 PM
I partially solved the problem with this:
#pragma strict
public var model:String;
function Start () {
var instance : GameObject = Instantiate(Resources.Load(model, GameObject),Vector3.zero,Quaternion.identity);
instance.AddComponent(BoxCollider);
}
but I don't really like the idea of having to create a box collider from script I simply would like to access the one I create in the inspector
Your answer
Follow this Question
Related Questions
Resources.Load for .Dat ? 1 Answer
Resources.Load() or Prefab manager? 1 Answer
Resources.Load from TextAsset string 1 Answer
Resources.Load makes Featureless Oddity 0 Answers
Resources.load absolute path 2 Answers