Search Unity

Trace Location of Scene Object's Script Instatiation

Discussion in 'Scripting' started by eshan-mathur, Feb 5, 2020.

  1. eshan-mathur

    eshan-mathur

    Joined:
    Nov 22, 2011
    Posts:
    118
    Hi all! I find myself running into this issue whenever I am stepping into a project or system I didn't write and need to trace what's going on - specifically, where certain objects are being instantiated.

    Is there a way to easily find out in which script the call to instantiate lives for a given scene object?

    I know about "Find References in Scene", but some of the objects that turn up in that result are actually a gigantic list of prefab references that all have lots of scripts and nested prefabs inside of them. You can't expand these prefab references so it would be an extremely tedious exercise to find them this way. And it still doesn't necessarily tell you where the call to Instantiate is.

    Using 2019.2, for what it's worth.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You could try putting a breakpoint in the Awake function of any component in the prefab. Awake runs during Instantiate, so I suspect (though I haven't checked) that you could find the originator by looking at the call stack from there.

    You could also try asking your IDE to "find references" to the "Instantiate" function. This may not be much help if there are a lot of calls to Instantiate() in the code base and you're not sure which one you need, but if there are only a handful then this will let you find them quickly.
     
    eshan-mathur likes this.