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 · Dec 02, 2013 at 07:09 AM · instantiateobjectfind-gameobject

Instantiate problems

HI, i try to instantiate an object from another scene, but the object never showed up because NullReferenceException

so basically i have 2 scene, CharSelection and GameWorld. here's the code that link them

this is CharacterSpawn.js that i attach to "GameWorld", MainCamera

 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 == 1){
             Instantiate(playerMale, Vector3(2,2,2), Quaternion.identity);
             playerFemale.SetActive(false);
             gameObject.AddComponent("PlayerStats");
             
         }
         else if(savedplayer == 2){
             //Instantiate(playerFemale, Vector3(0,0,0), Quaternion.identity);
             playerMale.SetActive(false);
             //playerFemale.SetActive(true);
             gameObject.AddComponent("PlayerStats");
         }
     }

this is ChangeChar.js attached to option button on "CharSelection"

 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 = -7;
         selectedplayer = 1;
         PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
     }
     if(isFemale == true) {
         PlayerAttributes.player_gender="Female";
         print(PlayerAttributes.player_gender);
         GameObject.Find("Cube1_M").transform.position.x = -7;
         GameObject.Find("testchar_F").transform.position.x = -1.7;
         selectedplayer = 2;
         PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
     }
 
 }

when i Instantiate i need playerMale as an GameObject.Find (the object to instantiate) but the GameObject.Find() need the object on the scene to point, and because my object was on CharSelection scene, it shows an error, which is NullReferenceException..

is there any way out of this loop?

Comment
Add comment
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

1 Reply

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

Answer by WilliamLeu · Dec 03, 2013 at 07:24 PM

If it's a template GameObject that's always the same, I'd suggest referencing a Prefab. They're game objects that are saved in the file hierarchy. You can assign them to GameObject references in the inspector like scene GameObjects. Their purpose is specifically to have Instantiate() called on them.

If it's a unique character that was created at runtime from another scene before the new scene was loaded, you could use DontDestroyOnLoad() on the GameObject. From there you can either call Instantiate() on it, or just reuse that existing GameObject that was created - but not destroyed - from the previous scene(s).

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 · Dec 03, 2013 at 08:03 PM 0
Share

yeah, you got the point, i want to use the character that was on another scene... whic is character creation scene..

therefor i use DontDestroyOnLoad()..

so i dont need to use Instantiate anymore?

and also, if i use DontDestroyOnLoad(), does the object appear somewhere on the scene? can i add some attribute after the selection?

avatar image Rgalaxy · Dec 03, 2013 at 08:08 PM 0
Share

oh, and where should i put DontDestroyOnLoad()? last time i put it on the character.. is it ok?

avatar image WilliamLeu · Dec 03, 2013 at 10:06 PM 0
Share

It doesn't matter when or from what script you call DontDestroyOnLoad() from, as long as it's called before the next scene is loaded and as long as it's called with the GameObject as the parameter that you want to keep from being deleted.

You don't need to use Instantiate() as long as you don't need to clone more of them.

The object should be where it last was, wherever its Transform behavior says it is. I'm guessing it will be kept where it was in world space before the new level loaded.

avatar image Rgalaxy · Dec 08, 2013 at 06:37 AM 0
Share

Thanks WilliamLeu :D

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

17 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

Related Questions

Plant Reproduction, Instantiate problem 1 Answer

How can I replace an object while keeping the references to that object? 2 Answers

Instantiated enemies not adding to kill count 1 Answer

Problem with Unity 0 Answers

Can't select shader help! 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