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 W1k3 · Jul 07, 2014 at 06:25 AM · c#errorinstantiatenull

"NullReferenceException: Object reference not set to an instance of an object" with instantiate C#

This must be something super simple that I'm missing but I couldn't find it for the life of me.

I have pasted my entire script, but the error is on line 48 when I try to add force to the instantiated object. The object has a rigidbody attached.

 using System.Collections;
 
 public class slimy : MonoBehaviour {
 
     public Rigidbody coin;
     float hp = 10;
     float defence = 1;
     public GameObject[] drops;
     public int[] droprate;
     public int mincoin = 1;
     public int maxcoin = 3;
     public Transform coinpos;
     public float moneyspread = 100;
 
     void OnCollisionEnter2D(Collision2D coll) {
 
         hitarrow ();
         deathcheck ();
     }
     void hitarrow(){
         float initial = playerattack.calculatedmg ("arrow");
         float dmg = initial / defence;
         hp -= dmg;
         Debug.Log ("Damage: " + dmg);
     }
 
     void deathcheck(){
         if (hp <= 0) {
             drop();
             Destroy (gameObject, 0.001f);
         }
     }
     void drop(){
         for(int i = 0; i < (Random.Range (mincoin, maxcoin)); i++){
 
             float coinforce = Random.Range(100f, 350f);
             float coinrot = Random.Range(0f, 360f);
             coinpos.localRotation = Quaternion.Euler(0, 0, coinrot);
             coinpos.position = transform.position;
 /*
             Rigidbody2D shot;
             shot = Instantiate(coin, coinpos.position, coinpos.rotation) as Rigidbody2D;
             shot.velocity = transform.TransformDirection(Vector2.up * coinforce);
 */
             Rigidbody clone;
             clone = Instantiate(coin, transform.position, coinpos.rotation) as Rigidbody;
             clone.velocity = transform.TransformDirection(Vector3.up * 10);
 
         }
     }
 }
 
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
1
Best Answer

Answer by AndyMartin458 · Jul 07, 2014 at 07:12 AM

The object you are creating has a Rigidbody, but it is not a Rigidbody. That is why the cast is not working out for you. You should instantiate the coin prefab as a GameObject and then use GetComponent to get the Rigidbody.

 GameObject go = Instantiate(coin, transform.position, coinpos.rotation) as GameObject;
 Rigidbody clone = go.GetComponent<Rigidbody>();
 clone.velocity = transform.TransformDirection(Vector3.up * 10);

You can find out more information here. http://docs.unity3d.com/Manual/InstantiatingPrefabs.html

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 W1k3 · Jul 07, 2014 at 07:22 AM 0
Share

This worked, thanks.

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

23 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

Related Questions

Can't find source of trigger Null Reference Exception 1 Answer

Instantiate object in C# 1 Answer

Can´t instantiate objects in list correctly 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars 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