Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

object reference not set to an instance of an object unity

Discussion in 'Scripting' started by Custardcs, Feb 19, 2016.

  1. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    Hi, Im new to this but getting the hang of everything at the moment when i run i get these errors.




    alright new i have 2 prefabs.. one is bullet tagd "Bullet" and the other is Zombie tagd "Zombie

    so they both will be instantiated on run time bullet from the gun when mouse 1 press and zombie well when its spawned.

    this is my Shoot.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shoot : MonoBehaviour {
    5.  
    6.     public GameObject bullet;
    7.     private GameObject bulletobstacles;
    8.     public GameObject bulletHole;
    9.     private GameObject bulletHoleobstacles;
    10.     public float delayTime = 8f;
    11.     private float counter = 0;
    12.  
    13.     public float Damage = 20f;
    14.  
    15.     public float bulletZomCurHealth;
    16.     // Use this for initialization
    17.     void Start ()
    18.     {
    19.         GameObject zombie = GameObject.Find ("Zombie");
    20.         Enemy_AI enemy_AI = zombie.GetComponent<Enemy_AI> ();
    21.         enemy_AI.ZomCurHealth = bulletZomCurHealth;
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update ()
    26.     {
    27.     }
    28.     //detect if ai is being shot
    29.     void OnCollisionEnter(Collision _BulletCol)
    30.     {
    31.         if (_BulletCol.gameObject.tag == "Bullet")
    32.         {
    33.             bulletZomCurHealth -= Damage;
    34.             print ("BulletShotZombie "+ bulletZomCurHealth);
    35.         }
    36.     }
    37.     void FixedUpdate ()
    38.     {
    39.         if (Input.GetKey (KeyCode.Mouse0)&& counter > delayTime)
    40.         {
    41.             bulletobstacles = (GameObject) Instantiate(bullet, transform.position, transform.rotation);
    42.             counter = 0;
    43.  
    44.  
    45.  
    46.  
    47.             RaycastHit bullethit;
    48.             Ray bulletray = new Ray (transform.position, transform.forward);
    49.             if (Physics.Raycast (bulletray, out bullethit, 100f))
    50.             {
    51.                 bulletHoleobstacles = (GameObject) Instantiate (bulletHole, bullethit.point, Quaternion.FromToRotation(Vector3.up, bullethit.normal));
    52.  
    53.  
    54.             }
    55.         }
    56.  
    57.         counter += Time.deltaTime;
    58.         //bullet destroy and time after spawn when its destroyed
    59.         Destroy (bulletobstacles, 1f);
    60.         //bulletHole destroy and time after spawn when its destroyed
    61.         Destroy (bulletHoleobstacles, 5f);
    62.     }
    63.  
    64. }
    65.  
    This is my Enemy_AI;
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy_AI : MonoBehaviour {
    5.  
    6.     private float fpsTargetDistance;
    7.     public float enemyLookDistance;
    8.     public float attackDistance;
    9.     public float enemyMoveSpeed;
    10.     public float enemyRotSpeed;
    11.  
    12.     public float DirChangeInt = 1;
    13.     public float MaxHeadingChange = 30;
    14.  
    15.     //zombie health
    16.     public float ZomMaxHealth = 0;
    17.     public float ZomCurHealth = 100;
    18.     //bullet damage
    19.     public int Damage = 20;
    20.  
    21.     //brainPrefab.
    22.     public GameObject zombieBrain;
    23.  
    24.  
    25.     Transform FpsTarget;
    26.     Renderer myRender;
    27.  
    28.     private Animator anim;
    29.  
    30.  
    31.     // Use this for initialization
    32.     void Start ()
    33.     {
    34.         anim = GetComponent<Animator> ();
    35.         myRender = GetComponent<Renderer> ();
    36.         FpsTarget = GameObject.FindGameObjectWithTag ("Player").transform;
    37.         ZomCurHealth = ZomMaxHealth;
    38.     }
    39.    
    40.     // Update is called once per frame
    41.     void Update ()
    42.     {
    43.         GameObject bullet = GameObject.Find ("Bullet");
    44.         Shoot shealth = bullet.GetComponent<Shoot> ();
    45.         ZomCurHealth = shealth.bulletZomCurHealth;
    46.         print ("zombie health " + ZomCurHealth);
    47.  
    48.         fpsTargetDistance = Vector3.Distance (FpsTarget.position, transform.position);
    49.         if (fpsTargetDistance < enemyLookDistance)
    50.         {
    51.             myRender.material.color = Color.yellow;
    52.             lookAtPlayer ();
    53.             //print ("Found Player");
    54.         }
    55.         if (fpsTargetDistance < attackDistance)
    56.         {
    57.             myRender.material.color = Color.red;
    58.  
    59.             ChasePlayer ();
    60.             //print ("Chase");
    61.             //Play The Animation
    62.             anim.Play ("Run1", 0);
    63.         }
    64.         else
    65.         {
    66.             wandering ();
    67.             //print ("wandering");
    68.             //Play The Animation
    69.             anim.Play ("Idle", 0);
    70.         }
    71.         //kill AI if health is below 0
    72.         if (ZomCurHealth <= 0)
    73.         {
    74.             Destroy (gameObject);
    75.             Instantiate (zombieBrain, gameObject.transform.position, Quaternion.identity);
    76.         }
    77.  
    78.     }
    79.        
    80.     void lookAtPlayer()
    81.     {
    82.        
    83.         Quaternion rotation = Quaternion.LookRotation (FpsTarget.position - transform.position);
    84.         transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * enemyRotSpeed);
    85.  
    86.     }
    87.     //chase the player
    88.     void ChasePlayer ()
    89.     {
    90.         transform.position += transform.forward * enemyMoveSpeed * Time.deltaTime;
    91.     }
    92.  
    93.     //wandering
    94.     void wandering ()
    95.     {
    96.         myRender.material.color = Color.blue;
    97.         //Play The Animation
    98.     }
    99.  
    100.  
    101.  
    102.  
    103. }
    104.  
    so at the moment i play its now running but throws the null error and i really dont understand how to set the bullet prefab to = bulletobstacles because that is the instantiated name of the bullet prefabs.
    thanks :) in advance
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    There are 3 errors in different places in your screenshot. The first one appears to be coming from Unity's internal UI code, maybe a corrupted .meta file or something - if it keeps happening, report it as a bug, but my gut tells me that's probably not going to affect you in a serious way. It's the other two that are more likely to be issues for you.

    The second error points to this line:
    Code (csharp):
    1.         Enemy_AI enemy_AI = zombie.GetComponent<Enemy_AI> ();
    Which means that "zombie" is null. "zombie" is set by... oh hey look, GameObject.Find! If you peek at my signature, you'll learn that I am no fan of GameObject.Find, and with many, many good reasons - basically, it is just never, ever the best solution to any given problem for runtime code. It's slow and unreliable - the slightest change or typo in the name of the object (including the "(Clone)" that gets tacked on when something is instantiated, for example) and it breaks, which is probably what's happening.

    From what I see here, a better solution might be to turn Enemy_AI into a singleton. (This assumes there's only one enemy at a time, which based on the code here, appears to be the case?)
    Code (csharp):
    1. public class Enemy_AI : MonoBehaviour {
    2. public static Enemy_AI instance;
    3.  
    4. void Awake() {
    5. instance = this;
    6. }
    7.  
    8. // replacing line 20
    9.         Enemy_AI enemy_AI = Enemy_AI.instance;
    10.  
    The third error looks to be the same thing in the other direction.
     
  3. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    thanks bud i changed them had to first understand what you ment lol you were in the difference places.

    umm at the moment there is only one zombie.. because im testing :| but i want it to spawn... lots.. but the moment im having a weird error on unity with triangles :| when i start its like 115k tris then play for a few secs and it just drops to 40k but nothing has be done, i have not put anything in or out of the scene :|

    so updated the two.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shoot : MonoBehaviour {
    5.     public static Shoot instance;
    6.  
    7.     public GameObject bullet;
    8.     private GameObject bulletobstacles;
    9.     public GameObject bulletHole;
    10.     private GameObject bulletHoleobstacles;
    11.     public float delayTime = 8f;
    12.     private float counter = 0;
    13.  
    14.     public float Damage = 20f;
    15.  
    16.     public float bulletZomCurHealth;
    17.  
    18.  
    19.  
    20.     void Awake()
    21.     {
    22.         instance = this;
    23.     }
    24.  
    25.     // Use this for initialization
    26.     void Start ()
    27.     {
    28.         GameObject zombie = GameObject.Find ("Zombie");
    29.         //Enemy_AI enemy_AI = zombie.GetComponent<Enemy_AI> ();
    30.         Enemy_AI enemy_AI = Enemy_AI.instance;
    31.         enemy_AI.ZomCurHealth = bulletZomCurHealth;
    32.     }
    33.    
    34.     // Update is called once per frame
    35.     void Update ()
    36.     {
    37.     }
    38.     //detect if ai is being shot
    39.     void OnCollisionEnter(Collision _BulletCol)
    40.     {
    41.         if (_BulletCol.gameObject.tag == "Bullet")
    42.         {
    43.             bulletZomCurHealth -= Damage;
    44.             print ("BulletShotZombie "+ bulletZomCurHealth);
    45.         }
    46.     }
    47.     void FixedUpdate ()
    48.     {
    49.         if (Input.GetKey (KeyCode.Mouse0)&& counter > delayTime)
    50.         {
    51.             bulletobstacles = (GameObject) Instantiate(bullet, transform.position, transform.rotation);
    52.             counter = 0;
    53.  
    54.  
    55.  
    56.  
    57.             RaycastHit bullethit;
    58.             Ray bulletray = new Ray (transform.position, transform.forward);
    59.             if (Physics.Raycast (bulletray, out bullethit, 100f))
    60.             {
    61.                 bulletHoleobstacles = (GameObject) Instantiate (bulletHole, bullethit.point, Quaternion.FromToRotation(Vector3.up, bullethit.normal));
    62.  
    63.  
    64.             }
    65.         }
    66.  
    67.         counter += Time.deltaTime;
    68.         //bullet destroy and time after spawn when its destroyed
    69.         Destroy (bulletobstacles, 1f);
    70.         //bulletHole destroy and time after spawn when its destroyed
    71.         Destroy (bulletHoleobstacles, 5f);
    72.     }
    73.  
    74. }
    75.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy_AI : MonoBehaviour {
    5.     public static Enemy_AI instance;
    6.  
    7.     private float fpsTargetDistance;
    8.     public float enemyLookDistance;
    9.     public float attackDistance;
    10.     public float enemyMoveSpeed;
    11.     public float enemyRotSpeed;
    12.  
    13.     public float DirChangeInt = 1;
    14.     public float MaxHeadingChange = 30;
    15.  
    16.     //zombie health
    17.     public float ZomMaxHealth = 100;
    18.     public float ZomCurHealth = 100;
    19.     //bullet damage
    20.     public int Damage = 20;
    21.  
    22.     //brainPrefab.
    23.     public GameObject zombieBrain;
    24.  
    25.  
    26.     Transform FpsTarget;
    27.     Renderer myRender;
    28.  
    29.     private Animator anim;
    30.  
    31.     void Awake()
    32.     {
    33.         instance = this;
    34.     }
    35.     // Use this for initialization
    36.     void Start ()
    37.     {
    38.         anim = GetComponent<Animator> ();
    39.         myRender = GetComponent<Renderer> ();
    40.         FpsTarget = GameObject.FindGameObjectWithTag ("Player").transform;
    41.         ZomCurHealth = ZomMaxHealth;
    42.     }
    43.    
    44.     // Update is called once per frame
    45.     void Update ()
    46.     {
    47.         GameObject bullet = GameObject.Find ("Bullet");
    48.         //Shoot shealth = bullet.GetComponent<Shoot> ();
    49.         Shoot shealth = Shoot.instance;
    50.         ZomCurHealth = shealth.bulletZomCurHealth;
    51.         print ("zombie health " + ZomCurHealth);
    52.  
    53.         fpsTargetDistance = Vector3.Distance (FpsTarget.position, transform.position);
    54.         if (fpsTargetDistance < enemyLookDistance)
    55.         {
    56.             myRender.material.color = Color.yellow;
    57.             lookAtPlayer ();
    58.             //print ("Found Player");
    59.         }
    60.         if (fpsTargetDistance < attackDistance)
    61.         {
    62.             myRender.material.color = Color.red;
    63.  
    64.             ChasePlayer ();
    65.             //print ("Chase");
    66.             //Play The Animation
    67.             anim.Play ("Run1", 0);
    68.         }
    69.         else
    70.         {
    71.             wandering ();
    72.             //print ("wandering");
    73.             //Play The Animation
    74.             anim.Play ("Idle", 0);
    75.         }
    76.         //kill AI if health is below 0
    77.         //if (ZomCurHealth <= 0)
    78.         //{
    79.         //    Destroy (gameObject);
    80.         //    Instantiate (zombieBrain, gameObject.transform.position, Quaternion.identity);
    81.         //w}
    82.  
    83.     }
    84.        
    85.     void lookAtPlayer()
    86.     {
    87.        
    88.         Quaternion rotation = Quaternion.LookRotation (FpsTarget.position - transform.position);
    89.         transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * enemyRotSpeed);
    90.  
    91.     }
    92.     //chase the player
    93.     void ChasePlayer ()
    94.     {
    95.         transform.position += transform.forward * enemyMoveSpeed * Time.deltaTime;
    96.     }
    97.  
    98.     //wandering
    99.     void wandering ()
    100.     {
    101.         myRender.material.color = Color.blue;
    102.         //Play The Animation
    103.     }
    104.  
    105.  
    106.  
    107.  
    108. }
    109.  
    the issue now tho is that this does not set the max health to the current health.?

    it should work but its just telling me health is 0.
     
  4. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    This needs to be clarified, is Shoot.cs on the bullet, zombie, or the player (I assume its the player).

    there are several things that need t be cleaned up in these scripts. first off each the scripts are responsible for too much data.

    This is all your shoot script should have. All it worries about is shooting the bullet let the bullet worry about hitting stuff.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shoot : MonoBehaviour
    5. {
    6.     public GameObject bulletPrefab;
    7.  
    8.     //works for all positive numbers under the frame-rate
    9.     public float fireRate = 8f;
    10.  
    11.     IEnumerator Start()
    12.     {
    13.  
    14.         while (true)
    15.         {
    16.             if (fireRate > 0 && Input.GetButton ("Fire"))
    17.             {
    18.                 GameObject bullet = GameObject.Instantiate<GameObject>(bulletPrefab);
    19.                 bullet.transform.position = transform.position;
    20.                 bullet.transform.rotation = transform.rotation;
    21.  
    22.                 yield return new WaitForSeconds(1/fireRate);
    23.             }
    24.             else
    25.             {
    26.                 yield return null;
    27.             }
    28.         }
    29.     }
    30. }
    The bullet will then have its own code that worries about hitting stuff.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bullet : MonoBehaviour
    5. {
    6.     public GameObject bulletHolePrefab;
    7.     public float damage = 20f;
    8.     public float maxRange = 100f;
    9.  
    10.     //is the bullet a hitscan (hits instantly), or one with a fast-moving projectile?
    11.     public bool hitScan = true;
    12.     public float speed = 100f;
    13.  
    14.     private Ray bulletRay;
    15.     private RaycastHit hit;
    16.     private float travelDistance=0;
    17.  
    18.     void Start()
    19.     {
    20.         bulletRay = new Ray (transform.position, transform.forward);
    21.      
    22.         if (hitScan)
    23.         {
    24.             if (Physics.Raycast (bulletRay, out hit, maxRange))
    25.             {
    26.                 CheckHit();            
    27.             }
    28.             else
    29.             {
    30.                 GameObject.Destroy (gameObject);
    31.             }
    32.         }
    33.     }
    34.  
    35.     void FixedUpdate()
    36.     {
    37.         if (travelDistance >= maxRange)
    38.         {
    39.             GameObject.Destroy (gameObject);
    40.             return;
    41.         }
    42.      
    43.         float fixedTravel = Time.fixedDeltaTime * speed;
    44.      
    45.         if (Physics.Raycast (bulletRay, out hit, fixedTravel))
    46.         {
    47.             CheckHit();            
    48.         }
    49.      
    50.         bulletRay.origin += bulletRay.direction * fixedTravel;
    51.         travelDistance += fixedTravel;
    52.     }
    53.  
    54.     void CheckHit()
    55.     {
    56.         if (hit.transform == null)
    57.             return;
    58.      
    59.         Enemy_AI enemy = hit.transform.GetComponent<Enemy_AI> ();
    60.         if (enemy == null)
    61.         {
    62.             GameObject bulletHole = GameObject.Instantiate<GameObject>(bulletHolePrefab);
    63.             bulletHole.transform.position = hit.point;
    64.             bulletHole.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    65.             Destroy (bulletHole, 5f);
    66.          
    67.             return;
    68.         }
    69.         else
    70.         {
    71.             enemy.Damage (damage);
    72.         }
    73.      
    74.         GameObject.Destroy (gameObject);
    75.     }
    76. }
    it is assumed that there is no bullet to render so the transform isn't updated in the FixedUpdate, just the raycast origin. ideally the bullet should check against a Damagable interface|script component when it deals damage (and a similar interface|script for the bullet holes) but to minimize the code here I'm going just specifically use the Enemy_AI

    finally the enemy AI should just focus on AI stuff. If anything would be a singleton it would most likely be the player so I wrote this class expecting you to have the Player class have a static method for getting the players transform.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Enemy_AI : MonoBehaviour
    6. {
    7.     public float enemyLookDistance;
    8.     public float attackDistance;
    9.     public float enemyMoveSpeed;
    10.     public float enemyRotSpeed;
    11.  
    12.     //zombie health
    13.     public float ZomMaxHealth = 100;
    14.     public float ZomCurHealth = 100;
    15.     //not being used?
    16. //    public float DirChangeInt = 1;
    17. //    public float MaxHeadingChange = 30;
    18.  
    19.     //brainPrefab.
    20.     public GameObject zombieBrain;
    21.  
    22.  
    23.     private Transform target;
    24.     private Renderer myRender;
    25.     private Animator anim;
    26.  
    27.  
    28.     void Awake()
    29.     {
    30.         anim = GetComponent<Animator> ();
    31.         myRender = GetComponent<Renderer> ();
    32.         ZomCurHealth = ZomMaxHealth;
    33.     }
    34.  
    35.     public void Damage(float amount)
    36.     {
    37.         ZomCurHealth -= amount;
    38.    
    39.         if (ZomCurHealth <= 0)
    40.         {
    41.             GameObject brains = GameObject.Instantiate<GameObject> (zombieBrain);
    42.             brains.transform.position = transform.position;
    43.             brains.transform.rotation = Quaternion.identity;
    44.             GameObject.Destroy(brains,20);
    45.  
    46.  
    47.             Destroy (gameObject);
    48.         }
    49.     }
    50.  
    51.     // Update is called once per frame
    52.     void Update ()
    53.     {
    54.         //make sure the player exists
    55.         if (target == null)
    56.         {
    57.             target = Player.GetTransform();
    58.             if (target == null)
    59.             {
    60.                 //default to wander aimlessly if there is no player
    61.                 Wander();
    62.                 return;
    63.             }
    64.         }
    65.  
    66.         float fpsTargetDistance = Vector3.Distance (target.position, transform.position);
    67.  
    68.         if (fpsTargetDistance < enemyLookDistance)
    69.         {
    70.             Alerted();
    71.             return;
    72.         }
    73.  
    74.  
    75.         if (fpsTargetDistance < attackDistance)
    76.         {
    77.             ChasePlayer ();
    78.             return;
    79.         }
    80.  
    81.  
    82.         //default to Wander mode
    83.         Wander ();
    84.     }
    85.  
    86.     void Alerted()
    87.     {
    88.         myRender.material.color = Color.yellow;
    89.         Quaternion rotation = Quaternion.LookRotation (target.position - transform.position);
    90.         transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * enemyRotSpeed);
    91.    
    92.     }
    93.     //chase the player
    94.     void ChasePlayer ()
    95.     {
    96.         myRender.material.color = Color.red;
    97.         transform.position += transform.forward * enemyMoveSpeed * Time.deltaTime;
    98.         anim.Play ("Run1", 0);
    99.     }
    100.  
    101.     //wandering
    102.     void Wander ()
    103.     {
    104.         myRender.material.color = Color.blue;
    105.         //Play The Animation
    106.    
    107.         anim.Play ("Idle", 0);
    108.     }
    109. }
    110.  
    here I also have the class have a damage method, I would recommend that this method and the current/maxHp be moved to a separate script because they honestly have no affect on the enemy's AI at all but for now I'm starting you off with just this
     
    Last edited: Feb 24, 2016
  5. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    Hi bud, thanks for the help...im not a coder i am an artist so i am teaching myself.. so when i run into errors i get confused as to why. even with your scripts i dont understand why exactly i must change certain things.

    but i saved the scripts and tested to see what difference it makes.. and so far.. i get 2 errors.

    in enemy_AI (target = Player.GetTransform; line 56) error = 'Player' does not contain a Definition for 'GetTransform'

    in Bullet (if (hit == null) line 56) error = 'Operator '==' cannot be applied to operands of type ''UnityEngine.RaycastHit" and "<null>"'


    i at the moment have 5 scripts..

    Bullet - attached to bullet prefab that is shot from the gun..
    DayNightController - day night cycle
    Enemy_AI - attached to zombie
    Player - attached to player. (holds max health current and damage.)
    Shoot - attached to player.

    thanks for the help bud.
     
  6. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    The reason you are getting an error in Enemy_Ai is as I said, I wrote it with the assumption that you would make the player class a singleton with the static method GetTransform. Singleton is a design pattern that programmers use to ensure that only one instance of a class can exist at a time.

    There should be at all times no more than one Gameobject with the Player script component (assuming this is a single-player game), so we make it singleton (as in the class should apply the singleton pattern). We do this by making the class use statics. statics is a keyword implying that the variable or method its linked with isn't tied to an instance of the class, but to the entire class itself. All instances of the class can have access to the same static variable/method as well as other classes.

    Ever notice how you didn't need an instance of the GameObject class to make a call to GameObject.Find()? thats because Find() is a public static method allowing other classes to call it without needing a instance of GameObject to call it with. Of course I avoid the use of GameObject.find like the plague since its slow (as it needs to check everything in the scene). I use singletons or a static list to efficiently find instances of components.

    Your Player class (along with the other stuff you haven't shown to be in the class) will need these lines of code.

    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.     private static Player instance;
    4.     void Awake()
    5.     {
    6.         if(instance !=null)
    7.         {
    8.             //player already exists, destroy the impostor instance
    9.             Destroy (gameObject);
    10.             return;
    11.         }
    12.  
    13.         //uncomment this if you want the same player instance to persist to the next level,
    14.         // which will keep data like current health and whatever else
    15.         //DontDestroyOnLoad(gameObject);
    16.  
    17.         instance = this;
    18.  
    19.         // put all the code you might have had in the Awake here
    20.     }
    21.  
    22.     // I could have overwritten the transform property but this will do.
    23.     public static Transform GetTransform()
    24.     {
    25.         if(instance == null)
    26.         {
    27.             return null;
    28.         }
    29.         return instance.transform;
    30.     }
    31.  
    32.     // add in all the other code you have in the class here
    33. }
    34.  
    granted this is not a true singleton in the definition of what a singleton is, then again we don't need a true singleton, just a way to limit the number of players in a scene.

    The error in the Bullet was my mistake as I forgot that RayCastHit is a struct not a class (so it can't ever be null), I'll edit my previous post and fix that
     
  7. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    alright so i read thru and kinda got an idea of what you are doing... but dont get how you reference the transform in player with no reference to the script?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.     public float PlayerMaxHealth = 100;
    6.     public float PlayerCurHealth = 100;
    7.     private static Player instance;
    8.  
    9.     public int Damage = 10;
    10.     void Awake()
    11.     {
    12.         if (instance != null) {
    13.             //player already exists, destroy the impostor instance
    14.             Destroy (gameObject);
    15.             return;
    16.         }
    17.         instance = this;
    18.     }
    19.     void start ()
    20.     {
    21.         PlayerCurHealth = PlayerMaxHealth;
    22.     }
    23.     void OnCollisionEnter(Collision _ZomCol)
    24.     {
    25.         if (_ZomCol.gameObject.tag == "Zombie")
    26.         {
    27.             PlayerCurHealth -= Damage;
    28.             print ("Zombie Poked You "+ PlayerCurHealth);
    29.         }
    30.     }
    31.     public static Transform GetTransform()
    32.     {
    33.         if(instance == null)
    34.         {
    35.             return null;
    36.         }
    37.         return instance.transform;
    38.     }
    39. }
    40.  
     
  8. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    What i dont get tho is its still not working and now the zombies dont chase the player..

    i get a null error if i keep

    target= Player.GetTransform;

    but

    it works when

    target = GameObject.FindGameObjectWithTag("Player").transform;

    but i know you are trying to explain why but i am more of a visual person unfortunately i am an artitst lol.

    so i get the basic concepts for what you are explaining, but i was shooting a prefab out of the gun but now.. u talking about ray casts.. instead of the bullet..

    then where does the bullet code code then if not on the bullet prefaB?
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    btw in your latest code post, you've got "start" not "Start"
     
  10. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    it should be
    Code (CSharp):
    1. target = Player.GetTransform();
    not
    Code (CSharp):
    1. target = Player.GetTransform;
    And as I said I don't need a direct reference to the script, just a reference to the class. And since the Player script is shared in the same namespace as your other scripts, the other scripts already know about the class' existence. then I grab the reference to the player's Transfrom from the static method GetTransform(). static is the important reason why this works.
     
  11. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    Thanks changed that and it seems to be working, so can i use static method with anything like the zombie health?

    umm and then one more question alright at the moment. in the Enemy_AI

    Code (CSharp):
    1. ChasePlayer ()
    it is not working...

    it seems to look at me but not chase me like it was before but the code seems the same?
     
  12. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    oh and forgot to say bullet spawn, spawns on the players postion and freezes