0
\$\begingroup\$

Currently I'm doing like this

foreach (var obj in DragAndDrop.objectReferences)
                {
                    GameObject go = GameObject.Find(obj.name);
                    
                    if (go != null)
                    {

                        GameObject parent = GameObject.Find("Parent");
                        if (parent == null)
                        {
                            parent = new GameObject();
                            parent .name = "Parent";                            
           
                        }
                        go.transform.SetParent(temp.transform);
                    }
                }

However, I try to drag something with the same name and it will be instantiated by other name(like A(1) rather than A), I can't find it with its name.

Also I try to create an instance by my own and put it to my parent node by code when I drag something into scene view. However, it seems unity will create an instance by itself so I will have two copies of instance in the scene view.

So how can I get the instantiated objects and put them to my parent node?

\$\endgroup\$
6
  • \$\begingroup\$ You could craete an empty GameObject Prefab first and use this with Instantiate to set your parent. \$\endgroup\$
    – Zibelas
    Commented Oct 25, 2021 at 8:11
  • \$\begingroup\$ I'm not quite clear about your meaning, could you please show the code? \$\endgroup\$ Commented Oct 25, 2021 at 8:17
  • \$\begingroup\$ I would advise avoiding looking up objects by name. There is almost always a more reliable distinguishing characteristic we can use, that's more efficient in runtime as a bonus. It's not clear to me from this snippet what object this script lives on, what objects it's iterating over, how they're instantiated, or what gameplay outcome this script is supporting. Can you edit your question to give us more of an overview of what you're trying to accomplish? \$\endgroup\$
    – DMGregory
    Commented Oct 25, 2021 at 9:52
  • \$\begingroup\$ I have no idea about getting the instance of a prefab when the user drags the prefab into the scene view. The code shows my failure. \$\endgroup\$ Commented Oct 25, 2021 at 10:35
  • \$\begingroup\$ What do you mean "user drags the prefab into the scene view?" That's something that can only be done in the Editor. Are you creating a script to be used by other members of a development team? \$\endgroup\$ Commented Oct 25, 2021 at 14:44

1 Answer 1

1
\$\begingroup\$

Eventually, I found two solutions.

if (currentEvent.type == EventType.DragExited){
    var goes = Selection.gameObjects;
    for (int i = 0; i < goes.Length; i++){
        DoSomething();
    }
}
if (currentEvent.type == EventType.DragExited){
    var objects = Object.FindObjectsOfType<MyScript>();//every prefab has MyScript component
    for (int i = 0; i < objects.Length; i++){
        DoSomething();
    }
}

Then you can get the instances of prefabs that the user drags into the scene view.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .