Why is reference to script object == null?

Greetings;

In C#, I have the following script:

TweenRotation _tweenRotation;
	void Start() {
		_tweenRotation = GetComponent<TweenRotation>();
		if (_tweenRotation == null) {
			print ("can't find _tweenRotation");
		}
	}

    void OnClick_btnSettings(){
    // this works just fine
	_tweenRotation.enabled = true;
}

This returns null. However, I can access the script just fine. The documentation from the following URL states this:

“function GetComponent (type : Type) :
Component Description Returns the
component of Type type if the game
object has one attached, null if it
doesn’t. You can access both builtin
components or scripts with this
function.”

Why is the object comparable to null if the object is valid and I can access the object just fine from within code?

Try something like this. Check if the Component is found before assigning it.

	TweenRotation _tweenRotation;

	void Start() 
	{
	    if(GetComponent<TweenRotation>())
	       _tweenRotation = GetComponent<TweenRotation>();
		
	    else
	    	print ("can't find _tweenRotation");
	}

Well, it’s also possible that you assign the variable elsewhere. To find out where your _tweenRotation get assigned, just turn it temporary into a property:

TweenRotation internal_TR = null;
TweenRotation _tweenRotation
{
    get{ return internal_TR; }
    set
    {
        Debug.Log("new value :" + value);
        Internal_TR = value;
    }
}

Now you can see in the console how often a new value is assigned and thanks to the stack trace you see from where it’s assigned.