Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 lucassivolella · Aug 17, 2020 at 11:30 PM · scripting problemscript.multiple objects

Multiple GameObjects with the same Script are sharing the same bool values when they shouldn't.

Hello everyone. I looked around similar topics, but could not learn what I needed. As the title says, I have a Script called StandardEnemyScript that holds the functions that all enemies share. One of these functions, FreezeEnemy(), is meant to freeze the enemy whenever it gets hit by a Freeze Arrow. It does it using a bool called canMove. The problem is that other enemies are sharing this bool value, so they also stop their movement.

I made the Script division because having the same functions for every king of enemy I created sounded like a bad idea, but now they are not working properly. Am I missing something important?

I'm posting the StandardEnemyScrip and another script called MeleeEnemyController, which holds the other functions that my melee enemies execute.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class StandardEnemyScript : MonoBehaviour
 {
     [Header("Enemy Movement")]
     [SerializeField] bool canMove = true;
     [SerializeField] float freezeMovementTime = 1f;
     [SerializeField] float freezeEnemyTime = 1f;
 
     private Color nativeColor;
     private Color freezeColor;
     private bool arrowFrozen;
 
     [Header("Enemy Health")]
     [SerializeField] float enemyHealth = 1f;
 
     [Header("Enemy Damage on Contact")]
     [SerializeField] Transform targetPlayer;
     [SerializeField] int enemyDamage = 1;
 
     // General Cached References
     LootDrop lootDrop;
 
     void Start()
     {
         lootDrop = FindObjectOfType<LootDrop>();
         targetPlayer = GameObject.FindGameObjectWithTag("Player").transform;
         nativeColor = GetComponent<SpriteRenderer>().color;
         freezeColor = Color.blue;
     }
 
     public void DamageEnemy(float incomingDamage)
     {
         enemyHealth -= incomingDamage;
 
         if (enemyHealth <= 0)
         {
             Destroy(gameObject);
             lootDrop.GetLootDrop();
         }
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.CompareTag("Player") && !arrowFrozen)
         {
             targetPlayer.GetComponent<PlayerController>().DamagePlayer(enemyDamage);
             StartCoroutine(FreezeMovement());
         }
         else if (collision.CompareTag("Player"))
         {
             targetPlayer.GetComponent<PlayerController>().DamagePlayer(enemyDamage);
         }
     }
 
     IEnumerator FreezeMovement()
     {
         canMove = false;
 
         yield return new WaitForSeconds(freezeMovementTime);
 
         canMove = true;
     }
 
     public void FreezeEnemy()
     {
         StartCoroutine(FreezeEnemyCoroutine());
     }
 
     IEnumerator FreezeEnemyCoroutine()
     {
         arrowFrozen = true;
         GetComponent<Animator>().enabled = false;
         GetComponent<SpriteRenderer>().color = freezeColor;
         canMove = false;
         yield return new WaitForSeconds(freezeEnemyTime);
         GetComponent<Animator>().enabled = true;
         canMove = true;
         GetComponent<SpriteRenderer>().color = nativeColor;
         arrowFrozen = false;
     }
 
     public bool CanEnemyMove()
     {
         return canMove;
     }
 }


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MeleeEnemyController : MonoBehaviour
 {
     [Header("Enemy Movement")]
     [SerializeField] float enemyMoveSpeed = 1f;
 
     private bool canMove;
 
     [Header("Enemy Damage")]
     [SerializeField] Transform targetPlayer;
 
     // Cached References
     StandardEnemyScript standardEnemyScript;
 
     // Start is called before the first frame update
     void Start()
     {
         targetPlayer = GameObject.FindGameObjectWithTag("Player").transform;
         standardEnemyScript = FindObjectOfType<StandardEnemyScript>();
     }
 
     // Update is called once per frame
     void Update()
     {
         ControllEnemyMovement();
     }
 
     private void ControllEnemyMovement()
     {
         canMove = standardEnemyScript.CanEnemyMove();
 
         if (targetPlayer && canMove)
         {
             transform.position = Vector2.MoveTowards(transform.position, targetPlayer.position, enemyMoveSpeed * Time.deltaTime);
         }
     }
 }


Thanks a lot for your help!

Comment
Add comment · Show 2
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 xxmariofer · Aug 18, 2020 at 12:43 PM 0
Share

you have only one instance of the script for all the enemies?

avatar image lucassivolella xxmariofer · Aug 18, 2020 at 01:41 PM 0
Share

@xxmariofer, no, every Enemy has this Script attached to it. I just found the answer to my problem, I'm posting it in a few $$anonymous$$utes.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by lucassivolella · Aug 18, 2020 at 02:00 PM

So, after a night of sleep I found the solution for the problem. The issue resided not on the StandardEnemyScript but actually on the MeleeEnemyController (and the other variants that I did not post in here).

On the Melee Script, Line 22, I had written:

 standardEnemyScript = FindObjectOfType<StandardEnemyScript>();

This was not working properlly, I believe, because FindObjectOfType was just looking for any object in the Scene that had the StandardEnemyScript attached to it, no matter if it was the very Object that was hit or some other Object that existed in the Scene. I changed the code to:

 standardEnemyScript = GetComponent<StandardEnemyScript>();

And the problem was gone, because now the Script is very specific that it is accessing the very Object it belongs. Of course, this is my theoretical explanation for the situation. I'm pretty amateur and lack experience with programming, C# and Unity, but this kind of situation feels like a game changer to me, because I am able to overcome some challenges and try some new and interesting stuff.

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

331 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 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 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

Scripting Help desperately needed. 1 Answer

how to change material on an object in unity 5 0 Answers

Health pickup script issue -1 Answers

How can i set the camera to be automatic behind the player ? 0 Answers

What is the new Get Tag From Raycast???? 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