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 /
  • Help Room /
avatar image
0
Question by snuffysam · Sep 18, 2015 at 02:04 AM · errorinstantiatespawn

Script component seems to be missing when spawning prefab?

I'm making a racing game, and trying to spawn the characters into the stage as the match starts.

The spawning object has an array named racers, which contains all the prefabs for the characters, and an array named spawners, which has all the locations where racers can spawn. There is also a game object called "GameController" which has all the data for what players should spawn.

 public class SpawnRacers : MonoBehaviour
 {
 
     public GameObject[] racers;
     public GameObject[] spawners;
 
     // Use this for initialization
     void Start ()
     {
         Destroy (GameObject.FindGameObjectWithTag ("Player"));
         var r = GameObject.FindGameObjectWithTag ("GameController").GetComponent<SpawnDataScript> ();
         int start = (spawners.Length - r.GetPlayers ().Length) / 2;
         for (int i = 0; i < r.GetPlayers ().Length; i++) {
             GameObject g = (GameObject)Instantiate (racers [r.GetPlayers () [i] - 1], spawners [start + i].transform.position, Quaternion.identity);
             g.transform.Rotate(new Vector3(0,-100,0));
             if (i < r.GetHuman ()) {
                 g.tag = "Player";
                 GameObject.FindGameObjectWithTag ("MainCamera").transform.SetParent (g.transform);
             } else {
                 g.tag = "Racer";
             }
             Debug.Log (g.tag);
             Debug.Log (g.name);
             Debug.Log (g.GetComponent<MovementScript>().special);
             Debug.Log (GameObject.FindGameObjectWithTag ("Player").name);
             Debug.Log (GameObject.FindGameObjectWithTag ("Player").GetComponent<MovementScript>().special);
         }
     }

The player is spawning in the right place, and most of the Debug.Log's at the bottom print the correct result, but the player won't move, and the error "Object reference not set to an instance of an object" shows up when running lines 27 and 29 (the ones that try to access MovementScript).

From this it seems like MovementScript is missing on my player's prefab, but it's definitely there in the inspector... what's going on?

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 _Yash_ · Sep 18, 2015 at 08:50 AM 0
Share

It seems that prefab is not updated after changing object. Select the instance of prefab in inspector and click the 'Save' button above Transform component.

avatar image snuffysam _Yash_ · Sep 18, 2015 at 01:16 PM 0
Share

There is no save button in the prefab- probably because I never changed the object.

avatar image snuffysam _Yash_ · Sep 20, 2015 at 05:27 AM 0
Share

Are there any other issues which could cause this problem? I can post a download link to the project if it will help.

avatar image snuffysam _Yash_ · Sep 21, 2015 at 05:34 PM 0
Share

Here's the project I'm working on: http://www.mediafire.com/download/2k1htu34zmq6125/Dragon_Ball_R.zip Let me know if there's anything you need explained.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Maruder · Sep 22, 2015 at 11:12 PM

You are getting that error probably because you destroy the player gameobject that holds the MovementScript.

Destroy (GameObject.FindGameObjectWithTag ("Player"));

Probably remove that line of code above and your problem may be solved. Good luck.

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 snuffysam · Sep 23, 2015 at 12:11 AM 0
Share

That code is run before the player GameObject is being created. I have a dummy GameObject with the player tag in the game, so that other objects that may run their start functions first don't crash the game.

And, like I said in the original question, the player isn't missing. I can see it on screen.

Regardless, I tried commenting out that line, but nothing was fixed.

avatar image
0

Answer by snuffysam · Sep 25, 2015 at 05:45 PM

I think I'll be creating a new question soon, since clearly the bigger problem (and likely the cause of this one) is that prefabs won't save.

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

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

32 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

Related Questions

Why do I keep getting 'The object you want to instantiate is null' warning? 1 Answer

Trying to spawn enemies on only one path 1 Answer

Networking - How do i spawn an object with client authority? 1 Answer

Spawning limited GameObjects at a specific position not working 1 Answer

spawn object every 10 score points 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