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 /
avatar image
0
Question by njwerner16 · Jul 28, 2018 at 12:44 AM · gameobjecttagsendmessage

How can I fix this so it sends the messages to the game objects with the tag?

hello! I know that questions similar to this have been answered but I am kind of new to C# coding so I don't understand too much on how to fix my code so it sends the messages to the enemies, if you can help me it would be really helpful but here is my code:

 public static bool GameIsPaused = false;

 public bool isResumed = true;

 public GameObject pauseMenuUI;
 GameObject[] AIEnemies;



 private void Start()
 {
     AIEnemies = GameObject.FindGameObjectsWithTag("EnemyAI");
 }

 private void Update()
 {
     if(Input.GetKeyDown(KeyCode.Escape))
     {
         if(GameIsPaused)
         {
             Resume();
         }
         else
         {
             Pause();
         }
     }
 }

 void PauseandResumeAI ()
 {
     if(isResumed)
     {
         foreach (GameObject enemy in AIEnemies)
         {
             enemy.SendMessage("Pause");
         }
     }
     else if(!isResumed)
     {
         foreach (GameObject enemy in AIEnemies)
         {
             enemy.SendMessage("UnPause");
         }
     }
 }

 public void Resume ()
 {
     pauseMenuUI.SetActive(false);
     Time.timeScale = 1f;
     GameIsPaused = false;
     Cursor.visible = false;
     Cursor.lockState = CursorLockMode.Locked;
     isResumed = true;
     GameObject.Find("MainCharacter").SendMessage("EnableHealth");


 }
 void Pause()
 {
     pauseMenuUI.SetActive(true);
     Time.timeScale = 0f;
     GameIsPaused = true;
     isResumed = false;
     GameObject.Find("MainCharacter").SendMessage("DisableHealth");
 }

I would love it if you could help me, thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by _jonathan_ · Jul 28, 2018 at 04:30 AM

I think you might have wanted to get the script on your enemyObjects that contains the SendMessage Method

 public static bool GameIsPaused = false;
  public bool isResumed = true;
  public GameObject pauseMenuUI;
 public EnemyObjectScript[] enemyScripts; // The name of your enemyObjects script
 
  private void Start()
  {
      GameObject[] AIEnemies = GameObject.FindGameObjectsWithTag("EnemyAI");
      enemyScripts = new EnemyObjectScript[AIEnemies.Length];
 
     for(var i=0;i<AIEnemies.Length;i++)
    {
      enemyScripts[i] = AIEnemies.GetComponent<EnemyObjectScript>();
    }
  }
  private void Update()
  {
      if(Input.GetKeyDown(KeyCode.Escape))
      {
          if(GameIsPaused)
          {
              Resume();
          }
          else
          {
              Pause();
          }
      }
  }
  void PauseandResumeAI ()
  {
      if(isResumed)
      {
          foreach (EnemyObjectScript enemy in enemyScripts)
          {
              enemy.SendMessage("Pause");
          }
      }
      else if(!isResumed)
      {
          foreach (EnemyObjectScript enemy in enemyScripts)
          {
              enemy.SendMessage("UnPause");
          }
      }
  }

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 njwerner16 · Jul 28, 2018 at 02:41 PM 0
Share

Hello, I still have a problem, it says that getcomponent can't be used there and also the enemies script is called PausingEnemy.

avatar image JVene njwerner16 · Jul 28, 2018 at 05:35 PM 0
Share

A $$anonymous$$or problem in the post is this:

  for(var i=0;i<AIEnemies.Length;i++)
 {
   enemyScripts[i] = AIEnemies.GetComponent<EnemyObjectScript>();
 }

Ins$$anonymous$$d, it probably should be:

  for(var i=0;i<AIEnemies.Length;i++)
 {
   enemyScripts[i] = AIEnemies[i].GetComponent<EnemyObjectScript>();
 }


And, of course, most code posted to you is example, A$$anonymous$$A pseudo code - you'll have to substitue the correct class names

avatar image
0

Answer by Skyfall106_Gaming · Jul 29, 2018 at 03:14 PM

Try this. Haven’t tested it so idk

  void PauseandResumeAI ()
  {
      if(isResumed == true)
      {
          foreach (GameObject enemy in AIEnemies)
          {
              enemy.SendMessage("Pause");
          }
      }
      else if(isResumed == false)
      {
          foreach (GameObject enemy in AIEnemies)
          {
              enemy.SendMessage("UnPause");
          }
      }
  }
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.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

SendMessage to all *tag* within range 1 Answer

Health and ApplyDamage to tagged objects - Help 1 Answer

Can't Return the Tag of a Child Object 4 Answers

Message sender...how to know who send the message 1 Answer

Replace variable with new game object after it is made null - not working 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