I am trying to change the parent of an instantiated prefab from another script which does not contain the actual "Instantiate(prefab, parent) code. So in one script, I instantiate prefab into a parent and in another script I am trying to reach that instantiated prefab and change its parent. Is it possible?
When I try that, I am always having the error of “setting the parent of a transform which resides in a prefab asset is disabled to prevent data corruption”.
You’re attempting to change the prefab itself, not the instance that you made.
Instead, store a reference to what is returned by Instantiate() and change that.
If it’s in another script, the way you get it over is always this pattern:
Referencing variables, fields, methods (anything non-static) in other script instances:
REMEMBER: it isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.
Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.
That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.