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 QC_Scott · Jul 27, 2014 at 09:19 PM · c#physicsrigidbodyoncollisionenter

Don't understand why this on collision script doesn't work.

I don't know why this script doesn't work. Am I just misunderstanding something simple. This is the error I get: NullReferenceException: Object reference not set to an instance of an object. This is my script:

using UnityEngine; using System.Collections;

public class ForceCalculator : MonoBehaviour {

 public float angle = 0;

 public GameObject Player;


 // Use this for initialization
 void Start () {
     Player = GameObject.Find ("player");
         }
 
 // Update is called once per frame
 void Update () {
 }
 
 public void OnCollisionEnter(Collision col){
     if(col.gameObject.name == "Weapon"){
         Player.GetComponent<PowerBar>().Force = (Player.GetComponent<PowerBar>().OriginalRotation - transform.Find("player").eulerAngles.y) * Player.GetComponent<PowerBar>().thePower;
         Player.rigidbody.AddForce(-transform.Find("player").forward * Player.GetComponent<PowerBar>().Force);

} } }

Please don't link me to script reference. I've read it and it hasn't helped. Please help. I have updated the script with improvements you've given me so far. But I still get the same error.

Comment
Add comment · Show 13
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 Hazlo · Jul 27, 2014 at 10:26 PM 0
Share

Hi! One first thing to note is that, performance-wise, it is not very nice to call "GameObject.Find("player")" during normal runtime (it is a slow call). Try to do it in Start or Awake and keep the object reference. Said that, can you isolate the object that is null? You have 2 lines of code full of Find(), ".rigidbody", "PowerBar" (that could not be initialized)... Try to set them in different lines and see which one reports the exception. Then we will continue to seek the null object. A possibility could be that you are calling "transform.Find" where you would need to do "GameObject.Find"... You'll see that if you isolate the exception, we'll find the cause really fast.

avatar image meat5000 ♦ · Jul 28, 2014 at 01:07 AM 0
Share

You need to create a reference to PowerBar before you can use it, unless it's static.

avatar image Yofurioso · Jul 28, 2014 at 01:14 AM 0
Share

Also, make sure your player game object is actually called "player" and not "Player"; It happened to me once :s

avatar image QC_Scott · Jul 28, 2014 at 10:07 AM 0
Share

Sorry, I'm a bit of a noob because I haven't done this in a while can you please re$$anonymous$$d me how to reference the script properly.

avatar image QC_Scott · Jul 28, 2014 at 10:15 AM 0
Share

I don't mean what command do I use, it's just I can't remember what kind of variable to store it as.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Nerull22 · Jul 28, 2014 at 02:34 PM

So, I'm not positive on what you're attempting to do, but there are a few options.

If your "PowerBar" is a data storage object, then you need a reference to it from the inspector (There are other ways, but that is the easiest). So after you have the reference to the instance, then you can quickly grab the variables associated to it.

You get the reference from the inspector by having...

 public PowerBar PowerBarInstance;

Then just reference PowerBarInstance.

Alternatively, if the PowerBar variables are on the game object that you just collided with, then you can simply put in...

 col.gameobject


in order to reference the gameObject that you just collided with, and you can go from there in order to get the information needed.

And in case you're wondering about how to get a component from the game object then look up the GetComponent method in the GameObject class for help with that. Hope this helps.

Comment
Add comment · Show 6 · 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 QC_Scott · Jul 28, 2014 at 02:54 PM 0
Share

The PowerBar is a script. Any ideas for that?

avatar image Nerull22 · Jul 28, 2014 at 03:25 PM 0
Share

Need more information. Is it a static script? Is it a script attached to a game object in the scene? Is it just a script lying around that hasn't been attached or instantiated to anything?

avatar image QC_Scott · Jul 28, 2014 at 05:01 PM 0
Share

It's a script that contains lots of variables which change throughout the program and it's attached to a game object called player. The script is called PowerBar and the variables I am trying to access are called: Force, Original Rotation and thePower. None of the variables are static.

avatar image Nerull22 · Jul 28, 2014 at 05:54 PM 0
Share

Alright, so in the script that you're trying to reference it, you want to add in the...

 public PowerBar PowerBarInstance;

And then in the inspector you drag your player object onto this inspector field. Then you just reference that public variable to get the variables. So you'll do....

 var value = PowerBarInstance.Force;

Or however you'd like to reference it.

I'd also recommend going to Unity's site and go to the "Leran" tab. Some of the tutorial videos on there will help you get started a little faster.

If you have more questions, feel free to ask.

avatar image QC_Scott · Jul 28, 2014 at 06:17 PM 0
Share

Thanks for the help. It made the script much better. It turns out there was a simple way of fixing it. The problem was to do with the transform. But your help has taught me a lot and helped me make the script more efficient. So thanks. And thanks to everyone who contributed to this question.

Show more comments
avatar image
0

Answer by QC_Scott · Jul 29, 2014 at 05:56 PM

Apparently the problem was in the way I worded the transform. It should have been written:' -Player.transform.forward' and not '-transform.Find("player").forward'

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

,Fast Moving Gameobject Collision and Trigger Check Failed 0 Answers

RigidBody immediately stops after AddForce 1 Answer

Fireing a tank projectile. 1 Answer

Unity Physics On Input Issue? 0 Answers

How would I get collision at different speeds? 2 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