Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Rgalaxy · Nov 26, 2013 at 04:34 PM · errorinstantiateobjectnullreferenceexception

[Closed]NullReferenceException on Object Instantiation onto game world

Here's my code

 var playerMale : GameObject;
 var playerFemale : GameObject;
 
 var savedplayer : int = 0;
 
 function Awake () {
     savedplayer = PlayerPrefs.GetInt("selectedplayer");
     
     playerMale = GameObject.Find("Cube1_M");
     playerFemale = GameObject.Find("testchar_F");
     
     if(savedplayer == 0){
     }
     else if(savedplayer == 1){
         playerMale.SetActiveRecursively(true);
         playerFemale.SetActiveRecursively(false);
     
     }
     else if(savedplayer == 2){
         playerMale.SetActiveRecursively(false);
         playerFemale.SetActiveRecursively(true);
     }
 }
 
 function Update () {
 
 }

I put that code on the Main Camera on Game Scene.

and this code goes to an object to choose Character (Male of Female) on CharSelection Scene.

 var isMale = false;
 var isFemale = false;
 var selectedplayer : int = 0;
 
 function OnMouseUp() {
 
     if(isMale == true) {
         PlayerAttributes.player_gender="Male";
         print(PlayerAttributes.player_gender);
         GameObject.Find("Cube1_M").transform.position.x = -1.7;
         GameObject.Find("testchar_F").transform.position.x = -5;
         selectedplayer = 1;
         PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
     }
     if(isFemale == true) {
         PlayerAttributes.player_gender="Female";
         print(PlayerAttributes.player_gender);
         GameObject.Find("Cube1_M").transform.position.x = -5;
         GameObject.Find("testchar_F").transform.position.x = -1.7;
         selectedplayer = 2;
         PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
     }
 
 }

but when i press button to load the Game Scene, the object did not appear at the scene, and the error is NullReferenceException on ActiveRecursively function...

could anybody help me out?

Comment
Add comment · Show 14
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 KiraSensei · Nov 26, 2013 at 04:36 PM 1
Share

Are Cube1_$$anonymous$$ and testchar_F active at the beginning ? If not, GameObject.Find("Cube1_$$anonymous$$") will return null

When you have an error, you should tell us at what line is the error. It's easier to find the problem.

avatar image Rgalaxy · Nov 26, 2013 at 07:15 PM 0
Share

yeph it is active, and currently be my object on CharSelection scene.. the line that eror is

player$$anonymous$$ale.SetActiveRecursively(false); playerFemale.SetActiveRecursively(true);

depends on what my char is (male or female) but the error is there, setactiverecursively(true); i confused what i wrong.. could u help ?

btw thanks for answering :)

avatar image KiraSensei · Nov 26, 2013 at 07:18 PM 0
Share

Try just SetActive ins$$anonymous$$d of SetActiveRecursively because it is deprecated. It will unable by propagation every child. If it is not the behavior you are looking for, we'll find something else.

avatar image Rgalaxy · Nov 27, 2013 at 05:10 AM 0
Share

i've try to change it, but the problem is still the same, maybe it have something to do with var i create? want to check out my unity project? (if u 're not busy)

or maybe there is another way to instatiate character to game from character selection screen?

avatar image KiraSensei · Nov 27, 2013 at 06:53 AM 0
Share

zip me your project, I'll have a look at it.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KiraSensei · Nov 28, 2013 at 07:48 AM

You just need to create a new script with :

 function Awake () {
     DontDestroyOnLoad (transform.gameObject);
 }

In it, and attach it to the "Character" game object, because it contains the two game objects you need. Or you can attach the script directly to Cube1_M and testchar_F, this should work too !

Comment
Add comment · Share
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
0

Answer by raimon.massanet · Nov 27, 2013 at 03:39 PM

GameObject.Find is something you should not use unless you have no other option. Instead, expose playerMale and playerFemale in the inspector, then drag and drop the objects from the scene.

Comment
Add comment · Show 4 · Share
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 Rgalaxy · Nov 27, 2013 at 03:43 PM 0
Share

could u explain me "expose player$$anonymous$$ale and playerFemale in inspector"? ermmhh, quite not understand thou.. sorry, it seems still a lot of thing i need to learn :)

avatar image KiraSensei · Nov 27, 2013 at 03:59 PM 0
Share

He says that you need to declare them public, and they will appear in the inspector, after that you just have to drag and drop the corresponding game object into the correct line in the inspector. But this works only if they already exist in the scene. If you instantiate them with another script, you need to use GameObject.Find

avatar image raimon.massanet · Nov 27, 2013 at 04:03 PM 0
Share

Exactly. Unity's way of working is like that. You should drag and drop in the Inspector window as much as you can. If the players are not in the scene, but ins$$anonymous$$d they are prefabs, it's O$$anonymous$$, you can drag and drop a prefab too and then Instantiate it.

avatar image Rgalaxy · Nov 27, 2013 at 05:16 PM 0
Share

so i can still instantiate it with drag and drop? so i create new var for player$$anonymous$$ and playerFwith type of GameObject?

thanks for explaining so far :)

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

19 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

Related Questions

After Instantiating a Prefab I get a NullReference Error even though the Object was created 2 Answers

NullReferenceException - When destroying Object 1 Answer

Error putting an object into an Array 0 Answers

NullReference Exception when adding a class 2 Answers

Instantiate GameObjects, brings an error to the log... 2 Answers


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