Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RouniXd · Dec 16, 2018 at 01:31 PM · referencingconsole errorsother script

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Dec 16, 2018 at 02:42 PM 0
Share

from what I see, $$anonymous$$enu$$anonymous$$anager is private.

avatar image RobAnthem · Dec 16, 2018 at 02:46 PM 0
Share

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.

avatar image RouniXd RobAnthem · Dec 16, 2018 at 03:52 PM 0
Share

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.

avatar image RobAnthem RouniXd · Dec 16, 2018 at 03:57 PM 0
Share

$$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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

97 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges