F
F
flupi21312412021-07-18 16:21:53
Android
flupi2131241, 2021-07-18 16:21:53

How to set a reference to an object instance in Unity?

In my game, by calling a method, a prefab clone is created. I threw a script on the prefab, from this script I turn to the variables of another script.
public Game ScriptGame;
An object is created only when a method is called and therefore I cannot designate another script, an error appears when calling:
NullReferenceException: Object reference not set to an instance of an object
60f42a30d5939207523170.png
60f42a5542809801423140.png
As I understand it, I need to somehow specify a reference to the script inside the script through this method, but how to do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AnanasikDev, 2021-07-23
@AnanasikDev

If this "other script" is one, then you can use the singleton pattern.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question