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

Changing a class type realtime

Discussion in 'Scripting' started by chrismkhill, Dec 12, 2015.

  1. chrismkhill

    chrismkhill

    Joined:
    Jan 12, 2015
    Posts:
    3
    Hi!

    So I'm trying to basically have a Space class, which has some basic space properties on a gameboard. But that space can be lots of things, like an obstacle or a gamepiece, a powerup, etc.

    So when designing the level, I click an enum and want that space to change to different type of space through an inherited subclass of Space.

    Then I want a separate class, say Gameboard to be able to see what type the class is and act appropriately.

    Psuedo-code
    Code (CSharp):
    1.  
    2. public GenericSpace<T> defaultSpace;
    3.  
    4. switch(spaceType)
    5. {
    6.  
    7. case (SpaceTypes.None):
    8.     defaultSpace = new GenericSpace<Space>();
    9.     break;
    10. case (SpaceTypes.Obstacle):
    11.     defaultSpace = new GenericSpace<Obstacle>();
    12.     break;
    13. case (SpaceTypes.Powerup):
    14.     defaultSpace. = new GenericSpace<Powerup>();
    15.     break;
    16. }
    17.  
    So everytime I change the enum in the editor, it becomes a different class. Then in gameboard I could say "in thoery" if defaultSpace.GetType() == Obstacle : do something

    I know its weird, but basically I have a space that can have different properties based on its type, and setting all these things manually on a switch case seems messy. An Obstacle having tons of properties and functions that itll never use also seems messy.

    Any help is greatly appreciate,
    Chris
     
  2. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
    To actually change the object into something else during runtime.. Not sure you can do exactly that, but the easy way out would be to delete the old Space and build a new one. You could write some code to "construct" a Space of a certain type from another Space. I'm not sure you necessarily need a GenericSpace<> class like that either but I admit that I can't always wrap my head around generics and effectively using them too easily.

    You could implement your own casting for different types of spaces, but it'll essentially be the same thing as I explained above. Read more about casting at https://msdn.microsoft.com/en-us/library/aa288476(v=vs.71).aspx if you are interested

    Code (csharp):
    1.  
    2. // NOTE, UnityEngine has its own interface called Space, so be careful about namespace clashes
    3. // You could make Space an interface or a class
    4. public class Space {}
    5.  
    6. public class EmptySpace : Space {}
    7. public class Obstacle : Space {}
    8.  
    9. void Start()
    10. {
    11.   List<Space> spaces = new List<Space>();
    12.   spaces.Add(new EmptySpace());
    13.   spaces.Add(new Obstacle());
    14.  
    15.   foreach (var space in spaces)
    16.   {
    17.     Debug.Log(space.GetType().ToString());
    18.     if (space is EmptySpace)
    19.     {
    20.      Debug.Log("ding");
    21.     }
    22.   }
    23. }
    24.  
    To clarify, you can use is to compare a type of an object to a class. You can make collections of a class and add objects of subclasses to it. C# is not dynamically typed, so I don't think you can change the actual type of something during runtime. You can always replace it with another one though.

    Code (csharp):
    1.  
    2. spaces[0] = Obstacle.From(spaces[0]);
    3.  
    or something, here From is a static builder or factory method that makes a new object of the type and initializes it from the object passed to it.
     
    Last edited: Dec 12, 2015
    Kiwasi likes this.
  3. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    If its a script you want to add to change the dynamics why dont you save it as a prefab and just load it in at run time?
     
    TonyLi and Kiwasi like this.
  4. chrismkhill

    chrismkhill

    Joined:
    Jan 12, 2015
    Posts:
    3
    Hmmm, this is interesting, the only thing is how do we expose that in the inspector now? that spaces[0] so that say the obstacle has it's own enum of its own type of obstacles that can be changed in the inspector. Huge thanks by the way!

    -Chris