- Home /
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.
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.
You need to create a reference to PowerBar before you can use it, unless it's static.
Also, make sure your player game object is actually called "player" and not "Player"; It happened to me once :s
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.
I don't mean what command do I use, it's just I can't remember what kind of variable to store it as.
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.
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?
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.
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.
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.
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'
Your answer
Follow this Question
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