• 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 j_basson · May 10, 2013 at 07:39 PM · instantiateprefab

Prefab in inspector not getting assigned

I'm trying to make an infinite runner game where enemy prefabs randomly spawn and move down the line, however unity keeps freezing and cras$$anonymous$$ng when I run the scene. T$$anonymous$$s is my controller.cs that will control the enemy creation and movement.

 public enum GameState { playing, gameover };
 
 public class Controller : MonoBehaviour {
 
     public ArrayList enemies;
     public static GameState gameState;
     public float targetsSpawnedUpTo = 0.0f;
     public Transform enemyPrefab ;//t$$anonymous$$s is giving me problems later on
     private Transform playerTrans;
     public float enemySpeed = 20.0f;
     
     void Awake()
     {
         enemies = new ArrayList();
         SpawnTargets(25.0f);
         StartGame();
     }
     
     void FixedUpdate()
     {
         
     }
     
     void StartGame()
     {
         Time.timeScale = 1.0f;
         gameState = GameState.playing;
     }
     
     void GameOver()
     {
         Time.timeScale = 0.0f;//pause
         gameState = GameState.gameover;
         //check score
     }
     
     void SpawnTargets(float upTo)
     {
         float spawnHeight = targetsSpawnedUpTo;    
         w$$anonymous$$le(spawnHeight <= upTo)
         {
             float x = Random.Range(-3.0f, 3.0f);
             Vector3 pos = new Vector3(x, 1f, spawnHeight);
             
             Transform badGuy = (Transform)Instantiate(enemyPrefab, pos, Quaternion.identity);//t$$anonymous$$s makes unity freeze and crash
             enemies.Add(badGuy);
             
             spawnHeight += Random.Range(-12.0f, -20.0f);
         }
         targetsSpawnedUpTo = upTo;
     }
 }

I've dragged my enemyPrefab into the inspector where it says "Enemy Prefab: None(Transform)" but then when I run the scene it freezes and gives me a "Object reference not set to an instance on the line where I instantiate the enemyPrefab.

Comment
Add comment · Show 2
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 Owen-Reynolds · May 10, 2013 at 08:16 PM 1
Share
avatar image j_basson · May 10, 2013 at 08:36 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Spawning a prefab at another object's location 3 Answers

Instantiate individual components of a prefab? 1 Answer

Issue Instantiating prefab in C# 0 Answers

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer

Instantiate a GameObject from a Prefab without using an original to clone 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