- Home /
How to attach script during runtime?
Hello, I am make a simple in unity. In that an object, which I am spawning through Instantiate method requires a reference to 2 scripts. So, how can I refer them to the object script while making them as private.
These are the scripts:-
private InterfaceManager interfaceManager;
private PlayerGameplay playerGameplay;
Answer by Nistroy · Mar 19 at 10:31 AM
These scripts are on other game objects, right?
I use 2 methods for that, either you search for the gameObject in question with :
GameObject.FindGameObjectWithTag("YourTag");
Or if there is only one script instance in the scene of your 2 scripts that you want to retrieve you can create a static of the 2 scripts there. For example in the InterfaceManager script you put the code like this:
public static InterfaceManager instance;
private void Awake()
{
instance = this;
}
And all you have to do is in the script of the instantiated object you get it in the start method like this:
void Start()
{
interfaceManager = InterfaceManager.instance;
}
Hope this help you !
Your answer
Follow this Question
Related Questions
How do I make the platform spawn after the other one 1 Answer
NullReferenceException: Object reference not set to an instance of an object ??? 1 Answer
character not moving forward 1 Answer
instantiate spawning meany objects in multiplayer 0 Answers
Is their any way to save a particle system when it's not instantiated yet? 2 Answers