• 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 thatkid05 · Oct 30, 2019 at 06:13 PM · prefabsunassigned

how do you assign a prefab to a script.

i am running an error saying : "UnassignedReferenceException: The variable asteroidPrefab of ShipBehaviour has not been assigned. You probably need to assign the asteroidPrefab variable of the ShipBehaviour script in the inspector." yet i have assigned it in the inspector panel. any answers?

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

Answer by BradyIrv · Oct 30, 2019 at 07:17 PM

A prefab can be assigned by dragging and dropping from the Project window. Did you do it this way or did you drag form the Hierarchy window?

Comment
Add comment · Show 2 · 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 thatkid05 · Oct 31, 2019 at 03:14 PM 0
Share

i dragged it from the project window, the prefab shouldn't be in the Hierarchy window, at least that's what my school course said for this assignment.

avatar image thatkid05 · Oct 31, 2019 at 03:31 PM 0
Share

public class ShipBehaviour : $$anonymous$$onoBehaviour {

 private float speed = 0.03F;

 public Text scoreText;

 public Text messageText;

 private int score = 0;

private bool gameOver;

 Teleporter porter;

 
 public GameObject laserPrefab;

 
 public GameObject asteroidPrefab;

   
 void Start()
 {
     
     porter = new Teleporter(Camera.main);

     // reset score text
     score = 0;
     scoreText.text = "0";

     
     createAsteroid();
     createAsteroid();
     createAsteroid();

     
     InvokeRepeating("createAsteroid", 1.0F, 3.0F);

 }

 
 void Update()
 {
     
     if (!gameOver)
     {
         
         transform.Translate(speed, 0, 0);

         
         float rotation = Input.GetAxis("Horizontal") * 5.0F;

         
         transform.Rotate(0, 0, -rotation);

         
         porter.Wrap(gameObject);

         
         if (Input.Get$$anonymous$$eyDown("space"))
         {
             
             fireShot();
         }
     }
 }

 void OnTriggerEnter2D(Collider2D otherObject)
 {
     
     string otherTag = otherObject.gameObject.tag;

     
     if (otherTag.Equals("asteroid"))
     {
         
         gameOver = true;
         messageText.text = "Game Over";
     }
 }

 
 public void IncreaseScore()
 {
     score++;
     scoreText.text = score.ToString();
 }

 
 void createAsteroid()
 {
     
     if (!gameOver)
     {
         
         float worldHeight = Camera.main.orthographicSize * 2.0F;
         float worldWidth = worldHeight * Camera.main.aspect;

         
         Vector2 position;
         position.x = Random.value * worldWidth - worldWidth / 2.0F;
         position.y = worldHeight / 2.0F;  

         
         Instantiate(asteroidPrefab, position, Quaternion.identity);
     }
 }

 
 void fireShot()
 {
     
     GameObject shot = (GameObject)Instantiate(laserPrefab, transform.position, transform.rotation);

     
     StartCoroutine(scheduleDestroy(1.0F, shot));
 }

 
 IEnumerator scheduleDestroy(float delay, GameObject target)
 {
     yield return new WaitForSeconds(delay);

     Destroy(target);
 }

}

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

192 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 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 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 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 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 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

For some reason I cannot drag my prefabs from the Project folder into the scene hierarchy. 2 Answers

How to redraw a texture on a prefab instance when a property in the inspector is changed by the developer? 0 Answers

Limit an amount of prefabs instantiated in javascript for Unity 5. 0 Answers

my instantiation /prefab of dan wont play death animations like orignal dan 0 Answers

how can i update the value of a variable that is in a prefab button without duplicating the prefab 0 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