I can't tell if I'm using GetComponent incorrectly or if I'm using some other part of unity wrong...

So I still want to complete this tutorial because I’m already about half way through but now I’ve run into another issue this time I’m trying to use GetComponent to enter a script that’s attached to a different game object. I’ve gotten get component working but I get the error :
NullReferenceException: Object reference not set to an instance of an object
ScriptProjectile.OnTriggerEnter (UnityEngine.Collider other) (at Assets/scripts/ScriptProjectile.js:38)

I initially thought that this was because the ScriptSceneManager (script being accessed) wasn’t attached to my player, it was attached to an empty gameObject named scene manager even though the player is where the ScriptProjectile (script trying to access) is. I thought this might be an issue because I’m not sure whether or not GetComponent can access information like scripts between game objects. But when I added the ScriptSceneManager to my player and disabled the other game object that originally had it attached I still got the error. I think I have GetComponnet right this time but now I can’t even figure out what the issue is. I tried to find tutorials about how to access a script between game objects and couldn’t find one on this specific situation and the script refrence isn’t helping at all even though I’ve tried all the examples. so if any one could possibly recommend a tutorial that would also be really great. But for now does anyone see any immediate issues?

Also the new part of ScriptProjectile starts at line 34.

This is ScriptProjectile

#pragma strict
//bullet script

//variables
var bulletSpeed     : float = 15.0; // speed bullet flys at
var destroyHeight     : float = 9;    // when the bullet is destroyed
var explosion         : Transform;
//var sceneManager    : GameObject;
var sceneManager    : scriptSceneManager;
//private variables

function Update () {
    transform.Translate(0,bulletSpeed * Time.deltaTime,0);
    if(transform.position.y >= destroyHeight){
    Destroy(gameObject);
    }
}

function OnTriggerEnter(other : Collider){
    print("THE DELICOUSNESS HAS LANDED!");


    //checks for asteroid
    if(other.gameObject.tag == "asteroid"){
    other.transform.position.y = 8;
    other.transform.position.x = Random.Range(-7.0,7.0);


    //creats explosion on impact
    if(explosion){
    Instantiate(explosion,transform.position,transform.rotation);
    }

    //Tell Scene Manager that we destroyed an enemy and add a point to the score
    //sceneManager.transform.GetComponent("scriptSceneManager").AddScore();
    sceneManager = GetComponent.<scriptSceneManager>();
    //sceneManager = GetComponent("scriptSceneManager");
    sceneManager.AddScore();




    //get rid of bullet
    Destroy(gameObject);
    }
}

This is ScriptSceneManager

#pragma strict
// Scene Manager Script

//Inspector var
//static var score : int = 0;
var score : int = 0;
var CounterGameTime :float = 60;

//Private var


//EveryFrame
function Update () {
    print("Score: " + score);


}

function AddScore(){
    score += 1;

}

Most likely you did not drag a reference to an extant object in the scene into some field in another game object.

Check over your tutorial steps and your work; it is likely you missed a critical dragged-a-reference step.

Line 36, get rid of the dot.

sceneManager = GetComponent<scriptSceneManager>();

Might just be a typo in this post though, as with the dot in there, it won’t run in the first place.

Also, is it ScriptSceneManager or scriptSceneManager? You’re using both in your description.

It’s ScriptSceneManager, sorry. I can’t find where I used scriptSceneManager but it is ScriptSceneManager. Also with the way it’s written in the post it runs until it’s called then it gives me the error I talked about. I tried to change it to:

//Tell Scene Manager that we destroyed an enemy and add a point to the score
    //sceneManager.transform.GetComponent("ScriptSceneManager").AddScore();
    //sceneManager = GetComponent.<ScriptSceneManager>();
    sceneManager = GetComponent<ScriptSceneManager>();
    //sceneManager = GetComponent("ScriptSceneManager");
    sceneManager.AddScore();

(I think you may be thinking of the C# version of GetComponent but I’m really not sure)
Like you said but now it gives me the errors:

Assets/scripts/ScriptProjectile.js(37,57): BCE0043: Unexpected token: ).
Assets/scripts/ScriptProjectile.js(37,58): BCE0044: expecting ), found ‘;’.
Assets/scripts/ScriptProjectile.js(37,59): UCE0001: ‘;’ expected. Insert a semicolon at the end.

As long as I leave the period in it only gives me the error I talked about earlier if I hit an asteroid with a bullet when it actually calls that line of code.

JS requires a dot between the method and the generic argument.

Your code looks for a scene manager on the bullet - which I’m sure is not what you meant. You need to call GetComponent on the GameObject that contains the scene manager script.

I think you’re probably right it’s just that for this part I had to branch off from my tutorial because in it the person doesn’t use #pragma strict so I decided to try and figure it out on my own. But now I can’t figure out how to make the bullet look into the game object (it’s just an empty gameobject that holds the ScriptSceneManager) that has the script.

Yeah, I have some idea of how to do that but I’m not really sure because I’ve looked through the scripting reference and it seems like everything on the GetComponent page is made to search only the gameObject that the script is attached to and I can’t figure out how to make it look in another game object… I want the bullet to use GetComponent to search into an empty gameObject named scene manager (no caps) then find the ScriptSceneManager.

You have some options

// find the thing directly
GameObject.Find("scene manager");
// make the scene manager a Singleton by caching a static reference to itself
// put this in ScriptSceneManager
public static ScriptSceneManager Instance;
void Awake()
{
    Instance = this;
}
// then access it in other scripts like this
ScriptSceneManager.Instance.AddScore();
// store a static private reference to the scene manager in the bullet
static ScriptSceneManager sceneManager;
void OnTriggerEnter(Collider other)
{
    if (sceneManager == null)
        sceneManager = GameObject.Find("scene manager").GetComponent<ScriptSceneManager>();

    sceneManager.AddScore();
}

Are there any JavaScript Options? :expressionless:

All of that is available in JS…

Oh, sorry about that. Yeah, I’m a C# boy. My mistake.

You code tags say C sharp? Right? maybe? Like could you send me to a link of some-sort explaining at least one of the options you wrote in Javascript; but explaining it differently than the scripting reference because when I tried that it still wouldn’t work and all the tutorials I’ve found so far don’t use #pragma strict and they use some weird method that won’t work if you do use #pragma strict.

No worries.

Honestly - if you’re having trouble translating that to JS then you might be better off starting here: http://unity3d.com/learn/tutorials/topics/scripting

Specifically this: http://unity3d.com/learn/tutorials/modules/beginner/scripting/c-sharp-vs-javascript-syntax?playlist=17117

Okay well before I totally give up here’s the example I think should work:

// You can access script components in the same way as other components.
    function Start () {
        var someScript : ExampleScript;
        someScript = GetComponent (ExampleScript);
        someScript.DoSomething ();
    }

\

This is the unity script reference example and I was wondering what I do with “ExampleScript” in my program I wrote :

var sceneManager    : ScriptSceneManager;
/*this(sceneManager variable) is in a variables section at the top of my script, is this okay or 
should I move it into the function where it's called? */

function OnTriggerEnter(other : Collider){
    print("THE DELICOUSNESS HAS LANDED!");


    //checks for asteroid
    if(other.gameObject.tag == "asteroid"){
    other.transform.position.y = 8;
    other.transform.position.x = Random.Range(-7.0,7.0);


    //creats explosion on impact
    if(explosion){
    Instantiate(explosion,transform.position,transform.rotation);
    }

    //Tell Scene Manager that we destroyed an enemy and add a point to the score
    //sceneManager.transform.GetComponent("ScriptSceneManager").AddScore();
    //sceneManager = GetComponent.<ScriptSceneManager>();

// these are the three that I'm trying the hardest with
    //sceneManager = gameObject.GetComponent.<ScriptSceneManager>();
    sceneManager = GetComponent(ScriptSceneManager);
    sceneManager.AddScore();




    //get rid of bullet
    Destroy(gameObject);
    }

(I left out function update because I don’t think there’s anything important in it)
After that’s created I go into the inspector window and I’ve tried to drag and drop the prefab of the scene manager, the scene manager that’s actually in the scene and the ScriptSceneManager. Is this even remotely close to what I’m supposed to do with the example script?

If you’re assigning sceneManager via the Inspector then there’s no reason to look for it again in your script. That makes your GetComponent call redundant (and still incorrect because you’re still looking for it on the same GameObject)