- Home /
How to detect if a game object is referenced in an unknown script
Ok, here's a tough question. I'm making an editor script and I will be destroying game objects and replace them with new game objects, my problem is that the old objects may be referenced in some scripts and I need the scripts to reference the new objects.
Here's what you need to know to provide an answer
1- I don't know which objects in the scene will be destroyed and replaced with a new one. And that means that I don't know which scripts I need to change their references.
2- I'm doing this in Editor time, the game is not running and I have some experience in Editor scripting
3- What I exactly need is a way to get an array of all references referring to a specific game object... is it even possible?
How do you not know what objects are being destroyed? At some point some script has to get a hold of the object you are destroying? Right? What is the overall goal you are trying to achieve? Perhaps you're going about it the wrong way.
I'm making a script that will replace Objects in Editor time, I don't know which Object the user will replace... so I have no idea if he has made a reference to this object he's replacing. Of course my script will catch a reference of the object before destroying it but what about any reference in the scene referring to it? they will all be null
Answer by Aspirer · Nov 09, 2013 at 01:52 AM
It would depend... I would say if you're going to be doing this via mouse, have your game-controller with an OnMouseDown call that draws a ray from the point of the click, in the direction the camera is facing (which should make a collision with whatever you're clicking on. It should be a GameObject. Then, I would assume something like
Destroy(HitCastInfo.collider.gameObject);
That's not what I'm asking for
I want to know if a specific game object is referenced in another script... any script.
And I'm doing this in Editor not in play time
OH! Totally misread the question. $$anonymous$$y apologies.
Answer by Serinx · Feb 15, 2021 at 03:53 AM
Bit of a necro, but I couldn't find an answer this question and ended up doing the following to figure it out. It's very long winded but fine to do if you only have to check a couple of references.
Copy the name of the gameobject you want to find the references for i.e. "BossSpawnpoint" (you might have to rename it if it's not unique).
Open the scene in file explorer using notepad
Ctrl+F Search for your "BossSpawnpoint"
You should find m_Name: BossSpawnpoint in the file. Slightly above should be something like this - component: {fileID: 1587079324}
Copy that ID and search for it - you should find that id referenced in another gameobject.
Scroll up a bit you should find the "GameObject:" tag and then a "m_Name:" which tells you the name of the GameObject which references your first gameobject e.g. "m_Name: BossSpawner"
Hope that helps someone! If anyone knows a better way, please let me know!