• 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 Guwang · Jan 11, 2016 at 01:18 PM · publicvoidanother script

Public void has problem while activing from another class.

Greetings! I want to active public void EnemyDestroyed in another class, Enemy class; but once this function is activated, in the console Unity says: NullReferenceException: Object reference not set to an instance of an object Enemy.Update () (at Assets/Scripts/Enemy.cs:51). In the inspector of enemyPrefab, where the Enemy script is attached to, there is a blank instance called Spawn Enemy, and there is no such thing that can be filled in that blank. In the script, monodevelope does consider SpawnEnenmy in Enemy script as a public class, but in the inspector it doesn't. I guess I did something wrong while calling the public void, but just couldn't figure it out by myself.

public class SpawnEnemy : MonoBehaviour {

 public Rigidbody enemy1;
 public Rigidbody enemy2;
 public Rigidbody enemy3;
 public GameObject SpawnPoint;
 public int enemyNum = 0;
 public int difficulty = 0;

 private float E = 0;

 void Refill ()
 {
     E += 1.0f * Time.deltaTime;
 }

 public void EnemyDestroyed ()
 {
     enemyNum -= 1;
     difficulty += 1;
 }

 // Update is called once per frame
 void Update () 
 {
     if (enemyNum < 7 && difficulty <= 10 && E >= 1)
     {
         Rigidbody enemy1Instance;
         enemy1Instance = Instantiate (enemy1, SpawnPoint.transform.position, SpawnPoint.transform.rotation) as Rigidbody;
         enemy1Instance.AddForce (SpawnPoint.transform.forward * 1);

         enemyNum +=1;
         E -= 3.0f;
     }

     if (enemyNum <= 5 && difficulty <= 30 && E >= 1 && difficulty > 10)
     {
         Rigidbody enemy2Instance;
         enemy2Instance = Instantiate (enemy2, SpawnPoint.transform.position, SpawnPoint.transform.rotation) as Rigidbody;
         enemy2Instance.AddForce (SpawnPoint.transform.forward * 1);

         enemyNum +=1;
         E -= 3.0f;
     }

     if (enemyNum <= 4 && difficulty <= 60 && E >= 1 && difficulty > 30)
     {
         Rigidbody enemy3Instance;
         enemy3Instance = Instantiate (enemy3, SpawnPoint.transform.position, SpawnPoint.transform.rotation) as Rigidbody;
         enemy3Instance.AddForce (SpawnPoint.transform.forward * 1);

         enemyNum +=1;
         E -= 3.0f;
     }

     if (E <= 3)
     {
         Refill ();
     }
 }

}

public class Enemy : MonoBehaviour {

 public SpawnEnemy Spawn;
 public int life = 200;
 public int moveSpeed = 1;

 private bool move;
 private int[] damage = {50, 25, 30};

 void Start ()
 {
     move = true;
 }

 void OnTriggerEnter (Collider other)
 {
     if (other.gameObject.CompareTag ("CannonBall")) 
     {
         life -= damage[0];
     }
     
     if (other.gameObject.CompareTag ("FireBall")) 
     {
         life -= damage[1];
     }
     
     if (other.gameObject.CompareTag ("IceBall")) 
     {
         life -= damage[2];
     }
     
     if (other.gameObject.CompareTag ("Gate"))
     {
         move = false;
     }
 }

 // Update is called once per frame
 void Update () 
 {    
     if (move == true)
     {
         transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
     }

     if (life <= 0)
     {
         Spawn.EnemyDestroyed ();
     }
 }

 void LateUpdate ()
 {
     if (life <= 0)
     {
         Destroy (gameObject);
     }
 }

}

Comment
Add comment · Show 1
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 gjf · Jan 08, 2016 at 11:47 PM 0
Share

to call it like that you need to make the method static, but a better way is to get a reference to the other script and call it that way.

look at GetComponent() in the docs.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by saschandroid · Jan 11, 2016 at 01:26 PM

Add a public variable for the game object that has the SpawnEnemy script on it to Enemy.cs

 public GameObject m_spawnEnemyObject;

and drag the game object with the script onto it in the inspector. Then add in Start() of Enemy.cs

     void Start()
     {
         Spawn = m_spawnEnemyObject.GetComponent<SpawnEnemy>();
     }





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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to call an int value from another void 1 Answer

public or private 4 Answers

Unity doesnt recognize public functions within a script 2 Answers

Trying to Invoke Void when Void has Stuff in Parameters - Easy Question 1 Answer

Unexpected symbol `void' 0 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