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 Kasper · Jan 27, 2011 at 08:04 PM · rigidbodynullreferenceexceptionbulletturret

NullReferenceException with a turret shooting a bullet

Ok i followed a tutorial on yotube: http://www.youtube.com/watch?v=fMUpJNndzcY
they thougt me how to rotate a turret towards the player n stuff. But now i'm trying to get the turrets to shoot the player. The scripting is the same as the guy on the tutorial(yeah i checked like 50 times). I've added a rigidbody, spawnpoint and a bullet. And i've added a delay to the shooting for like 1 second or something. And when i play the game i can play it for0,9 seconds then the bullet comes out of the turret and stands still in front of it giving me this error

NullReferenceException Turretcontrol.Shoot () (at Assets/Turretcontrol.js:29) Turretcontrol.Update () (at Assets/Turretcontrol.js:21)

Please help, i've already wasted a few hours of my life trying to solve it...

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 DaveA · Jan 27, 2011 at 10:38 PM 0
Share

Please post the JS here, so we can look at lines 21 and 29

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kasper · Jan 28, 2011 at 01:34 PM

Here it is :D, Ok i see that the code got really wierd somehow.

var LookAtTarget : Transform; var damp = 6.0; var bullitPrefab:Transform;

function Update() {
if(LookAtTarget) {

var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

      transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

     var seconds : int = Time.time;
     var oddeven = (seconds / 2);

     if(oddeven)
   {


     Shoot();
       }

 }           

}

function Shoot() { var bullit = Instantiate(bullitPrefab , transform.Find("spawnpoint").transform.position , Quaternion.identity);

      bullit.rigidbody.AddForce(transform.forward * 1000);

}

Comment
Add comment · Show 9 · 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 Phil 5 · Jan 28, 2011 at 02:31 PM 0
Share

Does your "bullitPrefab" have a rigidbody? Also, which are line 21 and 29?

avatar image Phil 5 · Jan 28, 2011 at 02:35 PM 0
Share

Also, does transform.Find("spawnpoint") return a transform? or null? transform.Find search for the transform inside his children. something like GameObject.Find("spawnpoint") would be more appropriate if the spawnpoint isn't child of the current object.

avatar image Kasper · Jan 28, 2011 at 02:35 PM 0
Share

yes it does and the eroor has move to only line 29 so thats were tha fault is

function Shoot() { var bullit = Instantiate(bullitPrefab , transform.Find("spawnpoint").transform.position , Quaternion.identity);

       bullit.rigidbody.AddForce(transform.forward * 1000);
  

}

avatar image Kasper · Jan 28, 2011 at 02:37 PM 0
Share

The spawnpoint is a child of the turret

avatar image Phil 5 · Jan 28, 2011 at 02:39 PM 0
Share

Call Instantiate(bullitPrefab, transform.Find("spawnpoint").transform.position, Quaternion.identity); At the start of Update() or in Start(). Just to test. If it fails there, try on the Find() part.

Show more comments
avatar image
0

Answer by Phil 5 · Jan 28, 2011 at 03:09 PM

var newPrefab : Transform = transform.Find("spawnpoint");
if (newPrefab)
{
    newPrefab = Instantiate(bullitPrefab , newPrefab.position , Quaternion.identity);
    if (newPrefab)
    {
        if (newPrefab.rigidbody)
        {
            Debug.Log("Bug is strange");
        }
        else
        {
            Debug.Log("No rigidbody in prefab");
        }
    }
    else
    {
        Debug.Log("Buggy Prefab");
    }
}
else
{
    Debug.log("Cant find");
}

Try this and tell me which output it gives. Put this in "Shoot()", and comment your current code in that function.

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 Kasper · Jan 28, 2011 at 03:20 PM 0
Share

Assets/Turretcontrol.js(27,18): BCE0043: Unexpected token: var. Assets/Turretcontrol.js(27,22): BCE0044: expecting EOF, found 'newPrefab'. this happened when i put it in the function shoot() when i put it in shoot() it gave me like 6 errors or something

avatar image Phil 5 · Jan 28, 2011 at 04:28 PM 0
Share

It's synthax error i think, i'm not used to javascript, EOF means End-Of-Fin

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

2 People are following this question.

avatar image avatar image

Related Questions

Problem with array on gameobjects 2 Answers

Scripting error comes up when I press play 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

ConfigurableJoint Anchors and nested RigidBodies 2 Answers

Rigidbody speed in some senes different?! 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