- Home /
GetComponent Problem
I've been working all night on this. I don't know what more to do... The most simple of things (getComponent) doesn't work and I'm a bit confused.
So, this is just a debug start of the script, 'cause fails in the most simple of things:
(this object and invChar object are the same, was just for looking for the problem. Setting public the script components doesn't neither helps
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(InvEquipment))]
public class AAL_CheckVisor : MonoBehaviour {
public GameObject invChar;
private InvEquipment allSlots;
private FlashLight testScript;
public int? itemID=null;
public GameObject visor1;
void Awake () {
//DEBUG
allSlots = this.GetComponent<InvEquipment> ();
if (allSlots != null) {
Debug.Log ("There isn't InvEquipment in object");
} else {
Debug.Log ("there is InvEquipment in objeto");
}
testScript = invChar.GetComponent<FlashLight> ();
if (testScript != null) {
Debug.Log ("No testScript i object");
} else {
Debug.Log ("testScript in object");
}
}
The Gameobject has the scripts attached. Everytime I run the game they report null. No need to say If I try to do something with them, I can cause the reference of the objects is empty.
So I'm lost. There is something clearly wrong? CAuse I don't have any idea why is unable to find anyobjects...
Thank you.
You print "There isn't InvEq in object" if allSlots is NOT null. Try Debug.Log(allSlots);
and see if that is null.
Are some of your scripts written in UnityScript and others in C#?
This might be reiteration, but do you know the syntax for GetComponent? In case you don't it's:
Your GameObject.GetComponent<Script On Your GameObject>(Arguments).Value You Need
For instance:
timeLeftOnThisScript = TimerObject.GetComponent<TimerScript>().timeLeft;
Other than that obvious note, I'm looking deeper into your code to see if I can find any bugs.
Answer by Calum-McManus · Mar 05, 2014 at 03:13 PM
if (testScript != null) {
Debug.Log ("No testScript i object");
} else {
Debug.Log ("testScript in object");
}
"!=" means not null, you are debugging "No testScript i object" when there is in fact a script on it.
Damm Yeah, I was changing the code so many times that thas was an error for my part. I have real problems with the real part of the script, I'll have put it all in here. I'll be back after rechecking all the script ;)