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

Monsters from spawner problem

Discussion in 'Scripting' started by JPCannon, Dec 9, 2014.

  1. JPCannon

    JPCannon

    Joined:
    Sep 25, 2013
    Posts:
    37
    Hi i have problem with monsters from my spawner. I`m trying to use freeze on my monsters. To do this if i hit some monster i change his speed to 0. After this i calculate few seconds in another method and change speed of my monster again on start value. First part works great, but after few seconds my script is changing speed of another random monster from spawner, not exacly freeze one. I think problem is related with Update. All this code is in HealthScript.cs in MonsterObjectPrefab. In the same object i have MovementScript.cs and i get it on start function. Like is write if i change speed only when i hit monster it works good, but if change is in update evene once it working on random object with the same name :p Is there some option to chenge speed only and exacly in this one monster object in MonsterScript.cs?

    Code (CSharp):
    1.    
    2. MovementScript move;
    3.    
    4.     void Start() {
    5.         move = GetComponent<MovementScript>();
    6. }
    7.  
    8. void OnTriggerEnter2D (Collider2D collider) {
    9.  
    10.         ShotScript shot = collider.gameObject.GetComponent<ShotScript> ();
    11.        
    12.         if (shot != null)
    13.         {
    14.         if (shot.isHit != isEnemy)
    15.             {
    16.                 PlayerData.hittedEnemys++;
    17.                 EffectOn(PlayerActiveSkills.actualActiveSkill);
    18.                 HP -= shot.damage;
    19.                 enemyHpBar.value = HP;
    20.  
    21.                 if(imOnIce)
    22.                     move.speed = new Vector2(0,0);
    23.  
    24.                 }
    25.         }
    26.    
    27.     }
    28.  
    29.     void EffectOn( SkillType effectType ) {
    30.  
    31.         if(effectType == SkillType.IceArrow)
    32.             imOnIce = true;
    33.     }
    34.  
    35. void OnIceEffect() {
    36.  
    37.         iceArrowTimeDuration[0] += Time.deltaTime;
    38.  
    39.         if(iceArrowTimeDuration[0] >= 1) {
    40.             iceArrowTimeDuration[1]++;
    41.             iceArrowTimeDuration[0] = 0;
    42.            
    43.             if(iceArrowTimeDuration[1] >= 10){
    44.  
    45.                 iceArrowTimeDuration[1] = 1;
    46.                 imOnIce = false;
    47.                 move.speed = new Vector2(150,10);
    48.                 Debug.Log("FReez End" + move.speed);
    49.  
    50.             }
    51.         }
    52.  
    53.     void Update () {
    54.  
    55.         if(imOnIce)
    56.             OnIceEffect();
    57.     }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Well, you're using GetComponent (implicitly on "this", i.e. the same object that's running the code) to get the MovementScript, so that's always going to be on the same object -- there's no way the code you've posted could affect some other object, regardless of its name or type. So, something else is going on.

    I do notice some funny things about this code, though. iceArrowTimeDuration appears to be an array, but you're using one element of it to actually track time, and the second element to count things. That's skanky. ;) It may not have anything to do with the problem you're observing, but who knows, once you let skanky things into your code anything can happen (often in baffling ways).

    So, I suggest refactoring that, and then if the problem still occurs, try to reproduce it in a very simple example you can post here in its entirety. Most likely, the exercise of making this simple example will lead you to the solution.

    Good luck!
     
  3. JPCannon

    JPCannon

    Joined:
    Sep 25, 2013
    Posts:
    37
    I now this isn`t nice solution but this working fine. If i dont use this in this way i must create one extra variable for any skill type, and for some even two more. That get me some about 10 variables not 20/25. This is only time counter and it`s working normal so i don`t think so this can help in this.

    For test i change my code. I add elements from MovementScreept to my HelthScript to have all in one place. I change my freeze method too on like this:

    Code (CSharp):
    1.     void OnIceEffect() {
    2.  
    3.             speed = new Vector2(0,0);
    4. }
    speed is monster tranform * Time.deltaTime. If i use it like this, all monsters from spawner stop moving. If i use this only in moment of arrow hit, only hitted monsters is stop :p I realy don`t understand whats wrong.
     
    Last edited: Dec 10, 2014