• 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 TobiUchiha · Aug 09, 2014 at 06:42 PM · getcomponentinstance

GetComponent suddenly stopped working

I have this one script which use get component to access another script.

 public class GameOver : MonoBehaviour {
     
     private ScoreScript scorescript;
     private BossEnemyScore besscript;
 
     void Start()
     {
         if (Application.loadedLevel == 1)
         {
             scorescript = GetComponent<ScoreScript>();
         }
         else
         {
             besscript = GetComponent<BossEnemyScore>();
         }
         Debug.Log (scorescript.Score);
     }
 //Other stuff
 }

The else part works quite perfectly(even the if part used to)... here's the script which I'm trying to access.

 public class ScoreScript : MonoBehaviour 
 {
     private Transform height;
     private Transform Player;
 
     public GUIText ScoreText;
 
     [HideInInspector]
     public float Score = 0f;
 
     private float HighScore;
 
  void Update()
     {
         if (height.position.y * 10 > Score) 
         {
             Score = Mathf.CeilToInt (height.position.y * 10);
         }
         ScoreText.text = "Score: " + Score;
 
         HighScore = PlayerPrefs.GetFloat("High Score");
 
         if (Score >= HighScore)
         {
             PlayerPrefs.SetFloat("High Score", Score);    
             HighScore = PlayerPrefs.GetFloat("High Score");
             Debug.Log("HighScore");
         }
     }

}

Here's the error that I have been receiving: Object reference not set to an instance of an object GameOver.Start () (at Assets/Scripts/Game/GameOver.cs:19)

Comment
Add comment · Show 2
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 tanoshimi · Aug 10, 2014 at 01:45 PM 0
Share

And your GameOver, ScoreScript, and BossEnemyScore scripts are all attached to the same object?

avatar image TobiUchiha · Aug 10, 2014 at 02:24 PM 0
Share

Nope, but they are in the same scene.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gjf · Aug 09, 2014 at 06:42 PM

if loadedLevel is not 1 then it's likely that the error will come from

 Debug.Log (scorescript.Score);

because scorescript has not been set to anything. it's good practice to check for null before using any references - in this case like:

 if (scorescript != null)
 {
     Debug.Log (scorescript.Score);
 }

btw, your error message relates to a line 19, which does not exist in the code you posted. did you remove something? it's important that you post accurate code when it refers to error messages otherwise our ability to help you is limited.

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 TobiUchiha · Aug 10, 2014 at 01:39 PM 0
Share

Thanks for the response, Line 19 is the line with Debug.Log(scorescript.score).. it isn't on line 19 as I skipped the directories and blank lines above. As I mentioned above I'm in scene 1. None of the above seems to solve the issues.

avatar image gjf · Aug 10, 2014 at 04:09 PM 0
Share

i'm not sure what "I skipped the directories and blank lines above" means, but if "Line 19 is the line with Debug.Log(scorescript.score)." then it's most likely that scorescript is null

avatar image
0

Answer by Bunny83 · Aug 10, 2014 at 04:20 PM

According to your answer to tanoshimi question above your scripts aren't attached to the same Gameobject. That's why GetComponent can't find the component

If you call just

 GetComponent<ScoreScript>()

You actually doing:

 this.GetComponent<ScoreScript>()


So you call GetComponent on your GameOver script instance. This will only find components which are attached to the same GameObject as your GameOver script is attached to. If they are on different objects, you have to find / get a reference to that GameObject first and use Getcomponent on the found gameobject.

If the scripts are all in the scene, it's easier to simply drop the GetComponent stuff all together and simply assign the script references in the inspector. For that you have to declare your variables as public and not private.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Access manually instantiated object 1 Answer

"Object reference not set to an instance of an object" Error 1 Answer

Using multiple instances of the same script 1 Answer

Referencing to an initiated Text 1 Answer

what the heck do you put in the object reference!? all i need is a variable! 2 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