Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by moonLite · Jul 02, 2012 at 07:06 PM · getcomponent

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moonLite · Jul 06, 2012 at 04:40 PM 0
Share

@$$anonymous$$elso$$anonymous$$R$$anonymous$$ , it's a book error, I have corrected it, thanks!

avatar image codejoy · Jun 29, 2013 at 11:53 PM 0
Share

I think I have the same book, and there is no answer in the errata, what was the answer?

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Jul 04, 2012 at 07:29 AM 0
Share

Actually Destroy is called at the end of the frame after the Update so that would cause a problem on the next frame only.

avatar image
0

Answer by moonLite · Jul 03, 2012 at 04:39 PM

@Wolin & @Viope ,

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Jul 03, 2012 at 06:34 PM 1
Share

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");
avatar image moonLite · Jul 06, 2012 at 04:39 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges