Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Im trying to learn how to access another scripts variable and classes of different direction, in Unity3D. But for some reason it keeps giving me the error.

NullReferenceException: Object reference not set to an instance of an object

public class Script_v2 : MonoBehaviour {

  public Vector3 size;

    public Player_Collission_Detection playerCollisionDetection;


    // Use this for initialization
    void Start () {

        playerCollisionDetection = GetComponent<Player_Collission_Detection> ();

        size = playerCollisionDetection.size;
    }

}

Here is the what the directory looks like.

Assets
     _Scripts
          _Enemy
               Script_v2.cs
          _Player
               Player_Collision_Detection.cs
share|improve this question

closed as off-topic by Anko, Byte56 Dec 30 '14 at 18:48

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

    
possible duplicate of How to import or "using" a custom class in Unity script? –  lase Dec 24 '14 at 17:13
    
I dont get what you mean –  JekasG Dec 24 '14 at 17:37

1 Answer 1

The null reference exception basically says that there is no script of type Player_Collision_Detection attached on the game object. Location of the script file doesn't matter if it's placed somewhere inside the assets folder of Unity.

Just find script from the inspector and drag it on the game object you are trying run this script.

share|improve this answer
    
But cant i just find the script through code ? And when you mean 'drag' what do you mean by that ? –  JekasG Dec 24 '14 at 18:41
    
Open up unity editor. There will be a tab named 'project'. Type in the name of the script; it will come up. Then, drag it on the game object from the hierarchy tab. –  Alican Dec 24 '14 at 18:46
    
But wouldnt that give the content of the script to the gameObject ? I just want to call variable from the script. –  JekasG Dec 24 '14 at 19:17
    
It creates a new instance from that script and adds it to the game object as a component. –  Alican Dec 24 '14 at 19:24
    
But wouldnt it inherent the script that was added ? I just want to reference one variable from that script and not the entire script itself. –  JekasG Dec 27 '14 at 10:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.