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?
Your answer
