Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Assigning components at run time.

Discussion in 'General Discussion' started by tclancey, May 23, 2020.

  1. tclancey


    May 19, 2017
    Hi folks. I've seen some posts on this subject, but not quite covering my concerns.

    I'm new to unity, I'm moving over from an other engine where I'm very at home swapping meshes and classes dynamically but running the main game code unchanged. It's fair to say I am not at all used to this drag and drop flow.

    I've been following some really good low poly racer tutorials, by Imphenzia (I think), I like his style, he explains things well. However, the work flow is create model, put model in game, drop all components on model.

    What if on level 2/3/4 I want the model to be different, car/ship/fruit/animal upgrade, from mini to BMW M3 or Apple to Zukini? How is that handled? I'm guessing this is a different work flow where a class script will control the model and properties. In that case does the object hierarchy work the same or would it have to be built on the fly? Would I just keep the same naming conventions for all the child objects? What if there are more or less child objects?

    From what I'm seeing at the moment (it hasn't been explained to me any other way) I have to build and place all the models, assign all child relations and code in the designer. This would be totally nuts for 20 different ships/cars, especially when they might not all be used.

    It does look as if pretty much everything can be exposed between code and designer, but I'm guessing there are limitations. Before I head off to start 5 days of work I'd like to know I haven't started out in the wrong direction.

    If this post makes sense to you and you kind of understand what I'm trying to get across, could you please suggest some tubes or reading material to cover these concerns?

    Many thanks.
  2. neginfinity


    Jan 27, 2013
    You can add components at runtime using Instantiate, Transform.SetParent, and AddComponent.

    Instantiate works best for spawning prefab, SetParent allows you to link objects into hieararchy, and AddComponent allows you to add any component to any object.

    Regarding spawning a different car... you prepare a different prefab for a different car in advance.
    The car is made out of generic components which make a car act like a car (a bunch of colliders and a controller script), and what kind of car depends on which componnts it is mad out of. For example, replacing body mesh with another mesh shouldn't affect behavior of car controller script, and same thing applies to changing mass, size of wheels or their position.

    There's no inherent coding difference between a monster truck and a kid's battery car. They have same parts, can have same script controlling them, what changes is configuration of the parts and meshes being used.

    That's the rough idea of it.
    Ryiah likes this.