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 zardini123 · Oct 06, 2013 at 04:01 PM · notworking

Instantiate not spawning prefab

I have a script that spawns a player when you press the play button from a different script. Everything is working fine, but the thing is the prefab isn't showing up. Heres the code:

 var playerGM : GameObject; 
 
 var startFlagPos : Vector3;
 var startFlag : GameObject;
 
 function Update () {
     if (WorldEditWindow.test == true) { 
         startFlag = GameObject.Find("Start Flag");
         if (!startFlag) {  
             Debug.LogWarning("There is no Start Flag in the level!");
         } else { 
             Debug.Log("Spawning Player...");
             startFlagPos = startFlag.transform.position;
             Instantiate(playerGM, startFlagPos, Quaternion.Euler(0,0,0));
             
             GameObject.Find("Main Camera").SetActive(false);
             GameObject.Find("VisualizerCube").SetActive(false);
             GameObject.Find("BuildGrid").SetActive(false);
             GameObject.Find("BuildGridBottom").SetActive(false);
         }
     }
 }    

I tested everything, and everything is working, the start flag is being found and such, but Instantiate is just not working in general.

Comment
Add comment · Show 7
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 robertbu · Oct 06, 2013 at 05:25 PM 0
Share

Can you see the object in the Hierarchy? That is, is it just not showing up in Game view, or is it not getting created at all?

avatar image zardini123 · Oct 06, 2013 at 06:41 PM 0
Share

The object is not being shown in the Hierarchy and the Game view. It looks like its not being created at all.

avatar image RyanZimmerman87 · Oct 06, 2013 at 06:48 PM 0
Share

I don't know what WorldEditWindow.test is. But you would be better of declaring the startFlag in Start()

GameObject.Find() in Update() can cause bugs, may be the cause of problem.

Also Instantiating is a pretty performance heavy operation if that is being called every Update() frame.

avatar image Hoeloe · Oct 06, 2013 at 07:36 PM 0
Share

Also, use Quaternion.identity ins$$anonymous$$d of Quaternion.Euler(0,0,0). You'll save many multiplications and a few trig calculations as it calculates to quaternion from the Euler angles.

avatar image zardini123 · Oct 06, 2013 at 08:39 PM 0
Share

WorldEditWindow.test is a static variable from script WorldEditWindow, which has a gui button that triggers WorldEditWindow.test boolean variable which then triggers all of the code above.

Show more comments

2 Replies

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

Answer by zardini123 · Oct 07, 2013 at 08:12 PM

Ok i got it to work! What I did is that when the Start Flag is spawned, I disabled that script that I said destroys the blocks thats in the same location. So now it works perfectly!

Its funny how people (thats me) panic but then it was a very dumb mistake! :)

Thanks all!

Comment
Add comment · Show 1 · 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 Hoeloe · Oct 07, 2013 at 08:14 PM 0
Share

It happens to everyone. This kind of error (false positive for bad state checks) crops up quite a lot, and it can be particularly nasty if you don't know to look for it.

avatar image
0

Answer by $$anonymous$$ · Oct 06, 2013 at 10:42 PM

Create a resources folder and asset's and pick apart the code I've used in another game:

If you miss the (Clone) bit instantiate adds, it can lead a person astray.

void OnLevelWasLoaded() { GetComponent ();

     if (OnSelectGui.maleDemonKnight == true){
     GameObject newPlayer = (GameObject)Instantiate(Resources.Load("DarkKnight"));
     }
     if (OnSelectGui.femaleDemonKnight == true){
     GameObject newPlayer = (GameObject)Instantiate(Resources.Load("DemonFem"));
     
     }


 void Start () {
     
 var spawn = GameObject.Find("spawn");
 var DarkKni = GameObject.Find("DarkKnight(Clone)");
 var DarkFem = GameObject.Find("DemonFem(Clone)");
 
 if (OnSelectGui.maleDemonKnight == true){
 DarkKni.transform.position = spawn.transform.position;
 }
 if (OnSelectGui.femaleDemonKnight == true){    
 DarkFem.transform.position = spawn.transform.position;
     }
 
 }
Comment
Add comment · Show 8 · 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 zardini123 · Oct 06, 2013 at 11:09 PM 0
Share

Look, the thing is that the gameObject im looking for, which is the Start Flag, is not in the scene when you load the scene. The user places the start flag, then when you press a button, itll spawn a player at the start flag.

avatar image $$anonymous$$ · Oct 06, 2013 at 11:17 PM 0
Share

Neither is $$anonymous$$e :), what this script does is instantiate based on a decision made in a different scene. So there's no tie together whatsoever and the character doesn't exist in the new scene.

So what it does then is look in my resources folder from void OnLevelWasLoaded()and dependent on what character is set to true, it then instantiates it to an empty gameobject placed in the scene I called spawn.

Update is way too late to be instantiating:

startFlag = GameObject.Find("Start Flag");

You have to call from resources, or where does it get Start Flag from?

Next it won't be called Start Flag, when you instantiate something Unity automatically adds Start Flag(Clone) on the end.

avatar image zardini123 · Oct 07, 2013 at 01:04 AM 0
Share

Ok, to get all of the shinanigans out of the way, Im gonna give you the sequence of what happens.

1) Scene Loads. Scene is empty.

2) Player creates scene by using the selected game objects. When the gameObjects are Instantiate, the script automatically removes the (Clone) at the end of the name with the code of:

 var thingSpawned : GameObject = Instantiate(Instantiate stuff goes here);
 thingSpawned.name.Replace("(Clone)", "");

3) When Player is ready, they press the Test button, which calls the script TestWorld which looks for the Start Flag in the scene that has been placed by the Player and spawns a PlayerGameObject at that location.

Also the playerG$$anonymous$$ is assigned through the Inspector in Unity.

avatar image Hoeloe · Oct 07, 2013 at 07:44 AM 0
Share

That's your issue - that bit of code. The method string.Replace doesn't do what you think it does. Strings are immutable (that is, once created, they can't be changed), so the only way to alter a string is to re-assign it. Replace doesn't change the existing string, it returns a new string that contains the new value, while the existing string remains the same - that code won't change thingSpawned.name. Try doing this:

 thingSpawned.name = thingSpawned.name.Replace("(Clone)", "");
avatar image zardini123 · Oct 07, 2013 at 07:46 PM 0
Share

Sorry i meant what Hoeloe typed. But seriously guys, the stuff you are telling me is wrong, like the name of the gameobject, isnt the problem. Thats NEVER been the problem! I've already done hours of debugging and crap and the Object in the Herhechiey (man i cant spell) is "Start Flag" not "Start Flag(Clone)", so please just stop about the name of the Object! Thats not the problem. Its the Instantiate thats causing the problems. All of the variables most likely are not even close to the problem. I used the Debug.Log method to track the result of GameObject.Find and it DOES find the Start Flag.

By the way, the Debug.Log("Spawning Player...") does initiate and does show in the log. So that concludes that the method is being called and the if statement is working. Its just the stupid Instantiate.

Show more comments

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

18 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

Related Questions

Game Works on Computer, but not on iPhone 0 Answers

Main Menu scrpit not working 1 Answer

Unity 3d android not working 1 Answer

Ocean not rendering (only during runtime) 1 Answer

Mono - Develop not working? 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