- Home /
Fail to getcomponent script from other object to work
Hi Guys,
I got an error message when my hero ship is trying to get script(Game_manager_script) function to pass & update the information (players live is zero, transform position). It happens when my hero spaceship is being hit by laser tag. And the error show up.
my hero ship is a prefab: "playership_prefab" attached with the script, "shipController.js"
My Game mananger is a empty game object," gameManger" attached with script, "gameManager_script.js"
The error message:
NullReferenceException
shipController.OnTriggerEnter (UnityEngine.Collider enemy)
(at Assets/_scripts/shipController.js:135)
My Hero ship, shipController.js
function Start () {
gameMgObj = GameObject.Find("gameManager");
}
function OnTriggerEnter(enemy : Collider)
{
if (enemy.tag == "enemyLaser" )
{
Destroy(this.gameObject);
// 1st try to getcomponent, but failed
var script1 = gameMgObj.transform.gameObject.GetComponent("gameManager_script");
//attempted to do
//var script1 :gameManager_script = gameMgObj.transform.gameObject.GetComponent(gameManager_script);
print("died");
var pPostition = transform.position;
script1.destroyPlayer(pPostition);
script1.respawn = true;
script1.playerLives -=1;
}
}
Below is my gameManager_script.js:
//destroy & respawn player
var playerObj : GameObject;
var playerSpwn : Transform;
var respawn = false;
var playerLives = 4;
var script1 : Component;
var player1 : GameObject;
var shield : GameObject;
var playerDeathObj : GameObject;
function destroyPlayer(dpos : Vector3)
{
var player1 : GameObject = Instantiate(playerDeathObj, dpos, playerDeathObj.transform.rotation) as GameObject;
player1.animation["death"].speed = 3.5;
yield WaitForSeconds(0.5);
Destroy (player1);
}
My questions is: 1) how to I solve? what error did I make?
2) I realize on my 1st script, shipController.js
var script1 = gameMgObj.transform.gameObject.GetComponent("gameManager_script");
Should I do without " " the Quotation mark? Because from the documentation, http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponent.html, there's one without, and with, when should I put?
3) Why do we use gameMgObj.transform.gameObject.GetComponent("gameManager_script");
by adding transform. I don't understand why. but be it without or with, it's still returning the same error.
instead of someScript = GetComponent (ExampleScript);? (i'm following a book, the book didnt really say much why)
Thanks in advance! Please let me know if I need to provide anymore information.
Answer by KelsoMRK · Jul 03, 2012 at 05:28 PM
Have you checked that your GameObject.Find() in Start() is actually finding a GameObject?
Concerning the other error - this occurs because you're using the string overload of GetComponent which returns a Component object instead of the type of your script. This overload requires an explicit cast to the type you are expecting. I would suggest using the Type overload or the Generic version of the method which will return a properly casted object.
EDIT: Lastly - why are you traversing down to game manager's transform and then back up to the GameObject itself? Just call GetComponent on the GameObject directly.
@$$anonymous$$elso$$anonymous$$R$$anonymous$$ , it's a book error, I have corrected it, thanks!
I think I have the same book, and there is no answer in the errata, what was the answer?
Answer by Wolin · Jul 03, 2012 at 01:48 PM
I think that you should not destroy 'this.gameObject' since it stores your gameMgObj reference. I might be wrong but maybe you should destroy it at the end of your function.
Actually Destroy is called at the end of the frame after the Update so that would cause a problem on the next frame only.
Answer by moonLite · Jul 03, 2012 at 04:39 PM
I left out the destroy game object statement, & tried various with the default getting componment methods, with/without the quotation marks, I tried to print script1.playerLives.
it said Assets/_scripts/shipController.js(100,23): BCE0019: 'playerLives' is not a member of 'UnityEngine.Component'.
And also it's still giving back the null reference exception Error.
Try and get rid of this weird GetComponent and use
var script:ScriptName=gameObject.GetComponent(ScriptName);
it could be this formula you are using is not giving anything. You could try to see if script1 is actually something with:
if(script1 == Null)print("Stupid book that gives wrong command");
@fafase ,
I found out, it's the getcomponent in the book has error. The correct version of the book should be:
var script1 = game$$anonymous$$gObj.gameObject.transform.GetComponent(game$$anonymous$$anager_script);
but I'm using the shorten and easier + more normal way of:
var script1 = game$$anonymous$$gObj.GetComponent(game$$anonymous$$anager_script);
Thanks guys!
FYI, Just for others new to unity too in future, using " " the Quotation mark is when you're getting from another language, for instance, javascript is trying to get script from C#. if not take out of the " " the Quotation mark.
Your answer
Follow this Question
Related Questions
C# - Problem with trigger that won't activate 1 Answer
Any way to 'GetComponent' as a base class? 2 Answers
Cannot get script component while the game object has a refered script 1 Answer
I Made a Revolver Script, But It's Saying There is an Error 2 Answers
Disable a GameObjects scripts knowing only the GameObjects name 3 Answers