- Home /
I can't drag prefab gameobject into other prefab gameobject
Hi guys,
I'm trying to make a path for enemy in my game. To do this I created a List variable named waypoints. Then I tried to drag waypoints into this. But I can't drag them into it.
public class EnemyPath : MonoBehaviour
{
[SerializeField] List<Transform> waypoints;
}
Thanks advance for your interest
Answer by blaskojoz · Nov 25, 2020 at 01:04 PM
I think you are trying to add values directly into the PREFAB. Instead of that, click on the enemy gameobject in hierarchy window and add some values here.
Yes I'm trying to do that. But Why doesn't it work ? For example when I serialize a gameobject, I can add values directly into prefab . So what is the problem with this list variable ?
And yes, As you said, I added values in the hiearchy, but I can't apply it to the prefab from "overrides" tab (When I click "Apply all" to save changes to prefab , nothing happens)
I'm not sure why it's not possible but if List(Gameobject) works why you simply don't use List(Gameobject) instead of List(Transform) ? Gameobject it's just simple wrapper/container for other components that derives from $$anonymous$$onoBehavor...
So in your code you can just simply get your transform like:
[SerializeField]
List<GameObject> waypoints;
public void Start()
{
waypoints.ForEach(gobj =>
{
var myTransform = gobj.GetComponent<Transform>();
// or better
var mySameTransform = gobj.transform;
});
}
my 2 cents : From my point of view, populating class fields should not be allowed from hierarchy window (when we talking about prefabs). Every gameobject in hierarchy window BELONGS to current scene...
So there is question: what would happen if you instantiate SA$$anonymous$$E prefab in different scene where waypoint gameobject does not exist?
Answer by alpayoguz97 · Nov 25, 2020 at 02:28 PM
No, not list (gameobject). Just gameobject. Creating gameobject and set reference in hierarchy works.
Your answer
Follow this Question
Related Questions
How to instantiate a prefab declared in one script, from another script 2 Answers
instantiated GameObject Prefab jumps in air after instantiate 1 Answer
Hiding gameobjects in hierarchy make it less expensive ? 0 Answers
prefab navagent doesnt work,prefab navmesh doesnt work 1 Answer
Prefab Instance changing when loading scene in editor 0 Answers