none
Find My [System.Serialized] class by name. RRS feed

  • Question

  • Hey there.

    So i have been spending my past week trying to get a Serialized.Class to respond too to a FindInstanceByName() extension.
    To be a little more specific, i'm trying to implement "ParticleProfiles", which hold a string name, int index, and list of particle systems available.
    My ParticleController is attempting to send one of its ParticleProfiles.particleList to another class.

    After tonnes of trial and error i am stumped.

    Script first problem after:

        public static class ParticleSystemExtensions
        {
    
    ///
    // I have tried many different if()' statements here.
    // Before the a and b method i directly tried 
    // if(prof.profileName == profName )
    ///
    
            public static ParticleProfile FindProfileByName(this ParticleProfile prof, string profName )
            {
                string a = prof.profileName;
                Debug.Log(a);
    
                string b = profName;
                Debug.Log(b);
    
    //
                if(a.Contains(b))
                    return prof;
    //or
                 if(a == b)
                    return prof;
    
    //or  if(a.StartsWith(b))
                     return prof;
    
    
    
                else
                    throw new System.Exception("prof not found");
    
    
            }
    
    
        }
    
        [System.Serializable]
        public class ParticleProfile
        {
    
           public string profileName;
           public int index;
           public List<ParticleSystem> particles = new List<ParticleSystem>();
    
       
     
    
        }
    
        public class ParticleController : MonoBehaviour
        {
            public List<ParticleProfile> ps_Profiles = new List<ParticleProfile>();
    
            RandomSystem particleRandomController;
    
    
            void Start()
            {
    
                particleRandomController = GetComponentInChildren<RandomSystem>();
                ChangeRandomParticles("PlayerEFX");
                
    
            }
    
            public void ChangeRandomParticles(string passProfile)
            {
    
                for (int i = 0; i < ps_Profiles.Count; i++)
                {
    
                    ParticleProfile send = ps_Profiles[i].FindProfileByName(passProfile);
                    List<ParticleSystem> pass = new List<ParticleSystem>();
                    pass.AddRange(send.particles);
    
                    foreach (ParticleSystem ps in pass)
                    {
                        particleRandomController.potentialParticles.Add(ps);
                    }
    
    
                }
    
                particleRandomController.Init();
    
            }


    My names do come out 100% the same yet no profile can be found. Please help.

    Monday, February 26, 2018 2:08 AM

All replies

  • Also i dont have those 

    //or actually in the script i just added that to show what ive tried before
    Monday, February 26, 2018 2:09 AM
  • Hello Spinehair,

    You are defining a extension method of ParticleProfile and it received a profName, return a ParticleProfile object. In ChangeRandomParticles method of ParticleController class. you invoke FindProfileByName method to get ParticleProfile  instance.

    >>My names do come out 100% the same yet no profile can be found. Please help

    I checked your code and it works well on my side. I'm not sure what happens in your situation. I suggest you could set breakpoint to your code and check variables step by step.

    https://docs.microsoft.com/en-us/dotnet/core/tutorials/debugging-with-visual-studio?tabs=csharp

    Testing code:

     class Program
        {
            static void Main(string[] args)
            {
                ParticleController controller = new ParticleController();
    
                controller.Start();
            }
        }
    
        public static class ParticleSystemExtensions
        {
            public static ParticleProfile FindProfileByName(this ParticleProfile prof, string profName)
            {
                string a = prof.profileName;
                //  Debug.Log(a);
    
                string b = profName;
                // Debug.Log(b);
    
                //
                if (a.Contains(b))
                    return prof;
                //or
                if (a == b)
                    return prof;
    
                //or  if(a.StartsWith(b))
                //  return prof;
    
                else
                    throw new System.Exception("prof not found");
            }
        }
    
        [System.Serializable]
        public class ParticleProfile
        {
            public string profileName;
            public int index;
            public List<ParticleSystem> particles = new List<ParticleSystem>();
        }
    
        public class ParticleSystem
        {
        }
    
        public class ParticleController 
        {
            public List<ParticleProfile> ps_Profiles = new List<ParticleProfile>() { new ParticleProfile { profileName="PlayerEFX"} };
    
           public void Start()
            {         
                ChangeRandomParticles("PlayerEFX");
    
            }
    
     
            public void ChangeRandomParticles(string passProfile)
            {
                for (int i = 0; i < ps_Profiles.Count; i++)
                {
                    ParticleProfile send = ps_Profiles[i].FindProfileByName(passProfile);                
                }
            }
        }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 27, 2018 6:10 AM
    Moderator