- Home /
Prefab loses associations in Resources folder
I rewrote my question: Object A needs to access the script on Object B. So in Object A's script I make a public variable for Object B and its script. After this I drag and drop Object B and its script into the appropriate spots in the Inspector for Object A. When I run it, it works fine. When I then put the Object A in the resources folder, the places in the inspector associated to Object B go blank. So I drag and drop them again, this time from prefab to prefab and it looks like it will work, but it doesn't. One difference I notice is that when I do it the first way, the associations are bold and in the prefab they are not.
$$anonymous$$y answer still applies to your question after the re-write. You can not link between objects saved as an asset and get them to work in a scene. The references point to a specific version of an object, so when you drag a reference from A to B in Resources, they still point to those resource versions after being put in scene. You need to get the references during runtime after they're instantiated.
As for the entries being bold, that points out a difference between a scene version of an asset and the prefab version.
Answer by OncaLupe · Oct 30, 2015 at 09:42 PM
References only work between objects within a scene, or on the same object when saved as an asset. You can't link between two asset objects.
Think of this example; If you could link in the assets and have 'a' pointing to 'b'. You then drag 'a' and two 'b' objects into the scene. Which 'b' should 'a' actually point to?
To do what you want(guessing you need these created during runtime), you'll need the objects searching the scene for the other object(slow but fairly simple), or use a third manager object to hold references. For instance you can have a GameController object that stays in the scene at all times, which has variables for 'a' and 'b'. When you instantiate 'a' or 'b', they set their respective values in the GameController and grab the value for the other object if it's set.
You can't link between two asset objects
This is a bit misleading. You can "link" between assets just fine. Of course if you instantiate a prefab the "link" / reference to another asset still points to the asset and not to some instance in the scene.
The question is a bit ambiguous what kind of "object" is ment. I guess the objects are objects in the scene and he linked them in the scene. When he dragged object A into the resources folder he actually created a prefab of the object A instance. Prefabs / assets can't reference objects in the scene so of course the reference to object B in the scene is removed in the prefab.
Then it seems that he also created a prefab out of object B and assigned the prefab of B to the variable in prefab A.
It's not really clear why he actually created prefabs out of the objects and if there are still instances of those prefabs in the scene or if they are instantiated at runtime.
Fact is scene objects are like in a nested scope of the project. So things in the scene can reference other things in the scene or things in the project (assets / prefabs). However assets / prefabs can't reference anything that is inside a scene.
An analogy could be a class and the methods inside that class. A class variable can be accessed from anywhere inside the class. However a local variable inside a method can only be used inside that method. The class would represent your whole project while the method(s) represent your scene(s). It might makes more sense if you consider that a scene doesn t have to be loaded all the time. If you switch from sceneA to sceneB all things inside sceneA are no longer available. Assets however don't belong to a scene. They are always available.
Answer by jacobjones · Nov 01, 2015 at 01:35 PM
Yes, now I understand. I was associating prefabs, instead of having the prefab's script look in the hierarchy with GameObject.Find("objectname").GetComponent(scriptname);
Thank you so much for the help.