- Home /
A problem regarding accesing a variable in other script.
Hello. So I am new to unity and I have encountered a problem that i dont know how to fix. So i am trying to make a platformer game with a different playable characters. So, the MenuMamager script saves the choosen character as a string, then send it to Initialization script, and this script looks for a gameobject with the same tag as string, and then instantiates it. The problem is very weird becouse i have made something similiar before and it worked. When I load the scene i get console error "object reference not set to an instance of object" in line 12. This is the whole code:
public GameObject player;
public string CharacterToInstantiate;
MenuManager menumanager;
void Start () {
CharacterToInstantiate = menumanager.ChosenCharacter;
player = GameObject.FindGameObjectWithTag(menumanager.CharacterToInstantiate);
Instantiate(player,transform.position,transform.rotation);
}
And this is 12 line:
CharacterToInstantiate = menumanager.ChosenCharacter;
Also in console I get a wartning, that the field MenuManager menumanager; is never assigned to.
So I dont know how to fix this. I dont get any red underlines, MenuManager script is public, ChosenCharacter is also public, and the menumanager script is set to NotDestroyOnLoad. Any help appreciated.
from what I see, $$anonymous$$enu$$anonymous$$anager is private.
So, your menumanager is technically a private member of this class, so it can never be assigned to in the editor. There are a few simple solutions though. If you wish to assign it in editor you simply add the keyword "public" in front of it so it says public $$anonymous$$enu$$anonymous$$anager menumanager
or you could add a line to your start function to locate it.
void Start () {
menumanager = FindObjectOfType<$$anonymous$$enu$$anonymous$$anager>();
CharacterToInstantiate = menumanager.ChosenCharacter;
player = GameObject.FindGameObjectWithTag(menumanager.CharacterToInstantiate);
Instantiate(player,transform.position,transform.rotation);
}
Alternatively you could look into singleton and have permanent static references to important objects.
Ok, so when I place 'public' before this line $$anonymous$$enu$$anonymous$$anager menumanager;
i get an error that object reference is not set to an instance of object, and when i place this line menumanager = FindObjectOfType<$$anonymous$$enu$$anonymous$$anager>();
ins$$anonymous$$d of 'public' i get an error, that the object i want to instantiate is null.
EDIT: i have just discovered, that when i use menumanager = FindObjectOfType<$$anonymous$$enu$$anonymous$$anager>()
i am able to debug.log menumanager.ChosenCharacter, so its a progress. For some reason i am unable to do this when i place 'public' before '$$anonymous$$enu$$anonymous$$anager menumanager'
Its seems like my script cant find a object with tag
EDIT2: Oh ok, it works! I just need to have an object with menumanager.CharacterToInstantiate on scene(hierarchy), not in the files. Thanks.
$$anonymous$$an I'd love to help you but you need to use some tutorials. The reason it said object reference not set is because you had to set it in inspector. The reason your instantiation was null is because you never defined the Player object you are instantiating at the line Instantiate(player,transform.position,transform.rotation)
now the reason for this is that you are "finding" the player object to instantiate, which won't work unless it already exists in scene, which is counterproductive to instantiating it. So you need a prefab of the player, a player variable in the class, and ASSIGN THE PLAYER or assign the player to menumanager as a member of its class, and reference it from the menumanager.
Your answer
Follow this Question
Related Questions
Reference specific variable from a specific gameObject? 2 Answers
public script reference that can be used for lots of scripts 1 Answer
Accessing a variable from one script in another. 2 Answers
multi reference a specific game object to a script that it's attached to many game objects ? 1 Answer
Get typeof component 1 Answer