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 Nekros28 · Oct 28, 2011 at 12:14 AM · javascriptinstantiatenullreferenceexceptionshoot

Instanciate Object Problem for Shooting

Hi all, i try to do my first shoot but i have one problem (I am new in Unity3D).

i Have this code:

 public var puntoDisparo: Transform;
 public var bala: GameObject;
 public var velocidad: float = 10;
 
 function Update () {
 
     if(Input.GetButtonDown("Fire1")){
     
         var cloneBala : GameObject = Instantiate (bala, puntoDisparo.position, Camera.main.transform.rotation);
         cloneBala.rigidbody.AddForce(new Vector3(0,0,velocidad), ForceMode.velocityChange);
     }
     
 }

Before add this line cloneBala.rigidbody.AddForce(new Vector3(0,0,velocidad), ForceMode.velocityChange); When i clic on left button mouse my gameObject is perfectly duplicated but however when i try to do that this gameObject can move foward i have this error:

NullReferenceException: Object reference not set to an instance of an object Disparo.Update () (at Assets/Scripts/Disparo.js:15)

'Disparo' is the script that i show before.. So.. What happend? my gameObject only move when i push it with others gameObjects (same gameObjects)

Here some pics.. In the Screen Shot UnityShoot you can see that only when i clicked 3 times if when one gameobject fly! :(

Please Help i want to keep learning

ScreenShots 125alt text125


http://www.dropbox.com/gallery/45494271/1/Screen%20Shots%20Unity3D%20FPS?h=317b52

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 syclamoth · Oct 28, 2011 at 12:26 AM 0
Share

Do you have a rigidbody on your prefab? It looks like it can't find the component! Otherwise, try putting an 'as GameObject' after your instantiation line (between the last bracket and the semicolon)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Oct 28, 2011 at 12:29 AM

Based on the pictures you provided, it should work - the only problem is ForceMode.velocityChange: it should be ForceMode.VelocityChange. Anyway, this code will make the bullet go in the Z direction; you should use cloneBala.transform.forward instead. You could also set rigidbody.velocity instead of using VelocityChange:

 cloneBala.rigidbody.velocity = cloneBala.transform.forward * velocidad;
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
avatar image
0

Answer by Nekros28 · Oct 29, 2011 at 07:38 AM

Mr. aldonaletto Thanks very much for help me.. when i change ForceMode.velocityChange for ForceMode.VelocityChange it's work but it not take the rotation of my camera (always shoot in one direction), but when i set there line for:

 cloneBala.rigidbody.velocity = cloneBala.transform.forward * velocidad;

All Fine! THANKS VERY MUCH.. But now i want to know why my old code not working, i'll read more for this and keep learning for understand what happend, one more time thnks for your time! your answer WORK PERFECTLY

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException firing bullets 1 Answer

Null Reference Exception when Trying to get script of newly instantiated Gameobject 1 Answer

I want to be able to make a good shoot script that actually works. 0 Answers

Shooting Script Instantiate Problems. Help? 0 Answers

iTween NullReferenceException error 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