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 /
This question was closed Oct 18, 2015 at 07:26 PM by Reaper1121 for the following reason:

The question is answered, right answer was accepted.

avatar image
0
Question by Reaper1121 · Oct 18, 2015 at 06:40 PM · c#instantiate

While instantiating getting object reference not set for no reason?

Hello. I want to instantiate a green ball and it does work but while doing that it shows me 2 same errors that say this: Object reference not set to an instance of an object. I have no idea why it would say that everything is correct and it does work like it should.

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class SimpleBulletShootAI : MonoBehaviour {
 
     private GameObject playerobj;
     public GameObject GreenPlasmaBall;
 
     private bool allowtoshoot = false;
     private float allowtoshoottimer = 3f;
     private float damping = 50f;
 
     // Use this for initialization
     void Start () {
     
         playerobj = GameObject.Find ("PlayerHuman");
 
     }
     
     // Update is called once per frame
     void Update () {
 
         // Shoot at player
         Vector3 lookPos = playerobj.transform.position - transform.position;
         lookPos.y = 0;
         Quaternion rotation = Quaternion.LookRotation(lookPos);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 
         if (allowtoshoot == false) {
             allowtoshoottimer -= Time.deltaTime;
         }
 
         if (allowtoshoottimer <= 0) {
             Rigidbody gunbulletinstance;
             gunbulletinstance = Instantiate(GreenPlasmaBall,transform.position,transform.rotation) as Rigidbody;
             gunbulletinstance.name = "GreenPlasmaBall";
             gunbulletinstance.AddForce(transform.forward * 5000);
             allowtoshoottimer = 3f;
         }
     
     }
 }
 

It says that the error is on line 36 that is: gunbulletinstance.name = "GreenPlasmaBall"; if i remove that line of code then it shows me the same 2 errors on line: gunbulletinstance.AddForce(transform.forward * 5000); . Soo how can i fix this false error? please help.

Comment
Add comment · Show 1
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 Halichoerus · Oct 18, 2015 at 06:53 PM 0
Share

Have you tried restarting unity? I get strange errors all the time when I edit prefabs in the editor, restarting unity usually fixes them.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by MountainDrew · Oct 18, 2015 at 07:05 PM

Try replacing "Rigidbody" with "GameObject" so that your code looks like this:

 GameObject gunbulletinstance;
 gunbulletinstance = Instantiate(GreenPlasmaBall,transform.position,transform.rotation) as GameObject;

And then you can just set the name like:

 gunbulletinstance.name = "GreenPlasmaBall";

or if you want to do it via the Rigidbody component

 gunbulletinstance.GetComponent<Rigidbody>().name = "GreenPlasmaBall";
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 MountainDrew · Oct 18, 2015 at 07:11 PM 0
Share

And then to add the force to your gunbulletinstance GameObject you need to get the Rigidbody component like so:

gunbulletinstance.GetComponent<Rigidbody>().AddForce(transform.forward * 5000);

avatar image Reaper1121 · Oct 18, 2015 at 07:25 PM 0
Share

It worked thank you soo much i did try GameObject first time it did not work but now when i tried it again it worked.

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

I got Some Error about how to Transform Postiton Enemy where one point can spawn 2 enemies. 1 Answer

How does one continuously move an object forward without using velocity? 1 Answer

Adding different values to randomly created objects 0 Answers

When re-parenting instantiated object as child of current gameObject, why does GetComponents() not retrieve instantiated object's components? 1 Answer

Prefab destroyed upon instantiation 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