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. Dismiss Notice

Bug Prefab is auto changing the scale to 0,1 when it should be 1,1

Discussion in 'Prefabs' started by n7pankake, Jun 21, 2023.

  1. n7pankake

    n7pankake

    Joined:
    Nov 18, 2017
    Posts:
    18
    Just as the title says I'm trying to make some dust trail or landing effects based on animation events and for some reason the prefab is working fine, no errors what so ever the thing is the prefab is spawning with 0,1 scale which means is visibly not showing up (This is 2D game) but the object does spawn I can even see it on the hierarchy and when I select the boject when I manually change the Scale to 1,1 you can see the animation prefab as it should. English not the best sorry for typos I'll attach images and code I'm using

    So this is what should Spawn:

    upload_2023-6-21_15-29-35.png

    This is what is spawning:

    upload_2023-6-21_15-31-1.png

    As you can see the prefab is indeed spawning which means the code is working I'm also running a Debug.Log to see what is coming out and it seems to be the correct information:

    upload_2023-6-21_15-36-1.png

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerAnimations : MonoBehaviour
    7. {
    8.     [Header("Effects")]
    9.     [SerializeField] GameObject runStopDust;
    10.     [SerializeField] GameObject jumpDust;
    11.     [SerializeField] GameObject landingDust;
    12.  
    13.     private AudioManager m_audioManager;
    14.  
    15.     // Animation Events
    16.     // These functions are called inside the animation files
    17.  
    18.     public void Start()
    19.     {
    20.         m_audioManager = AudioManager.instance;
    21.     }
    22.  
    23.  
    24.     void SpawnDustEffect(GameObject dust, float dustXOffset = 0)
    25.     {
    26.         if (dust != null)
    27.         {
    28.             // Set dust spawn position
    29.             Vector3 dustSpawnPosition = transform.position + new Vector3(0, 0.0f, 0.0f);
    30.             GameObject newDust = Instantiate(dust, dustSpawnPosition, Quaternion.identity) as GameObject;
    31.             // Turn dust in correct X direction
    32.             newDust.transform.localScale = newDust.transform.localScale.x * new Vector3(0, 1, 1);
    33.         }
    34.     }
    35.  
    36.     void AE_runStop()
    37.     {
    38.         m_audioManager.PlaySound("RunStop");
    39.         // Spawn Dust
    40.         float dustXOffset = 0.6f;
    41.         SpawnDustEffect(runStopDust, dustXOffset);
    42.     }
    43.  
    44.     void AE_footstep()
    45.     {
    46.         m_audioManager.PlaySound("Footstep");
    47.     }
    48.  
    49.     void AE_Jump()
    50.     {
    51.         m_audioManager.PlaySound("Jump");
    52.         // Spawn Dust
    53.         SpawnDustEffect(jumpDust);
    54.     }
    55.  
    56.     void AE_Landing()
    57.     {
    58.         m_audioManager.PlaySound("Landing");
    59.         // Spawn Dust
    60.         SpawnDustEffect(landingDust);
    61.     }
    62. }
    63.  
    That's the whole code of Player animations where sounds and animations prefabs effects are being controlled, theres an audio manager that all it does is to configure the sound lvs and names of the sounds effects

    Now coming to the issue this is the animations and the prefabs:

    upload_2023-6-21_15-42-29.png

    So the animation landing does have an event that is suppose to call AE_Landing where you already saw a code run and should make a noise and instantiate a prefab (with it does BOTH) however the prefab comes with scale 0,1 instead of 1,1.

    They do have a reference in the player.

    How I fix this bug because it seems to be a weird error or something cause no error is being displayed and for some reason it keeps coming out as 0,1 thanks.
     

    Attached Files:

  2. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    557
    You have it written out at line 32
    newDust.transform.localScale = newDust.transform.localScale.x *  Vector3(0, 1, 1);

    Would this not set the X scale to 0?
     
  3. n7pankake

    n7pankake

    Joined:
    Nov 18, 2017
    Posts:
    18
    God sake I must be blind I mean it was working before so that's probably why I didn't even look around there I can't remember changing that, ugh thank you
     
    Rin-Dev likes this.
  4. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    557
    No problem, we've all had it happen at least once