Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ekglag2 · Dec 02, 2015 at 10:03 AM · nullreferenceexceptionclassesmethodsfindobjectoftypeglobal script

When I call my script2 in script1 it makes script2 global? It stops me from accessing script2's methods at all.

I try to access my Shield script in my PlayerController script just like I usually do:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     
     private LivesKeeper livesKeeper;
     private Shield shield;
 
     void Awake(){
         livesKeeper = GameObject.FindObjectOfType<LivesKeeper>();
         shield = GameObject.FindObjectOfType<Shield>();
     }
 }


Now, the LivesKeeper script is called in as usual, when I hover over livesKeeper the pop-up says "LivesKeeper livesKeeper" but when I hover over shield the pop-up says "Global::Shield shield".

I have no idea what that means, and when I try to access a public method in the Shield script unity gives me an error in the console that states:

"NullReferenceException: Object reference not set to an instance of an object PlayerController.Shield () (at Assets/Scripts/PlayerController.cs:133) PlayerController.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/Scripts/PlayerController.cs:95)"

PlayerController.cs:95 refers to when I call the Shield() method in my PlayerController script after the shield power-up collides with the player ship. The shield appears just fine and exactly where I want it. Here is my Shield() method.

 public void Shield(){
         if(ScoreKeeper.score >= 13000){
             Debug.Log ("ShieldLvl3 Active");
             shieldLvl = 3;
             Instantiate(shield3, transform.position, Quaternion.identity);
             shield.ShieldActive();
         }else if(ScoreKeeper.score >= 8000){
             Debug.Log ("ShieldLvl2 Active");
             shieldLvl = 2;
             Instantiate(shield2, transform.position, Quaternion.identity);
             shield.ShieldActive();
         }else{
             Debug.Log ("ShieldLvl1 Active");
             shieldLvl = 1;
             Instantiate(shield1, transform.position, Quaternion.identity);
             shield.ShieldActive();
         }


PlayerController.cs:133 refers to the 'shield.ShieldActive();' line. The Shield script is a component of the shield I instaniate in the previous line of the code. I don't understand why it cannot access my Shield script or why it says it is global. Here is my entire Shield script, maybe the issue is in it...

 using UnityEngine;
 using System.Collections;
 
 public class Shield : MonoBehaviour {
     
     public Sprite[] shields;
     public int maxHits = 0;
     
     private PlayerController player;
     private Vector3 playerToShieldVector;
     private int timesHit = 0;
     
     void Awake () {
         player = GameObject.FindObjectOfType<PlayerController>();
         playerToShieldVector = this.transform.position - player.transform.position;
     }
     
     void Update () {
         //lock shield above player ship
         this.transform.position = player.transform.position + playerToShieldVector;
     }
     
     public void ShieldActive(){
         if(player.shieldLvl == 3){
             maxHits = 3;
         }else if(player.shieldLvl == 2){
             maxHits = 2;
         }else{
             maxHits = 1;
         }
     }
     
     void OnTriggerEnter2D(Collider2D col){
         Destroy(col.gameObject);
         if(timesHit == maxHits - 1){
             this.gameObject.SetActive(false);
         }else{
             timesHit++;
             LoadSprite();
         }
     }
     
     void LoadSprite(){
         if(timesHit == 1){
             this.GetComponent<SpriteRenderer>().sprite = shields[0];
         }else if(timesHit == 2){
             this.GetComponent<SpriteRenderer>().sprite = shields[1];
         }else {
             Debug.LogError("Shield sprite missing");
         }
     }
 }
 

Why is this happening to Shield but not to LivesKeeper and is it the reason why I cannot access the Shield methods through my PlayerController script?

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Method not working inside FixedUpdate 2 Answers

How to store variables in a separate class 1 Answer

Referencing uninstantiated GameObject within a class? 0 Answers

How can I change a function from another script? 1 Answer

How can I do a method that return a class? 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