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

Is it possible to instantiate gameobject without certain components?

Discussion in 'Scripting' started by ptgodz, Nov 18, 2016.

  1. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    Is it possible to instantiate a gameobject without specific components. I know I could instantiate the gameobject and then GetComponent<"">.enabled = false to deactivate components in the same frame but is it possible to do this without.

    Thanks
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    If you are creating a prefab and instantiating it, then no. You'll either have to remove the component after creation (or disable if you think you'll use it). Or create a prefab and add the component to it (or enable it if you create a prefab with the component disabled to start with).
     
  3. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    Thought so : /. Thanks for replying man
     
  4. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    On the same note, If I'm instantiating a group of objects through a for loop but some objects have 1 script attached and others have a different script, and I want to disable which ever script is attached to the gameobject. How would I go about this.

    In my head I'd need something like the contain method of a List. So I'd say if (gameObject.Contains "script") then disable

    Is there a work around anyone can think of?
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Why do you even instantiate it with the components that you don't want to have on it?
     
    rahulk1991 likes this.
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Well, if it's two scripts. You can have them inherit from an interface, thus you just disable the interface and it should disable that script. But if not that, then you'll have to probably use get component, see if the value is null, disable if it's not, disable the other script if it is.

    Or, if it's the same prefab, and it's named lets say zombie, just check the name of the gameobject to see if it contains zombie and you know you need to disable the zombieController script. Or whatever. But honestly, seems there would be better options. As @Suddoha mentions, what is the purpose of what you are trying to do?
     
  7. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    If you're instantiating mutated objects (as in - variations of the same base) and for some reason you don't want a prefab with all variations, you can do it the other way - store the base prefab and add components to it.

    Simple example (abstract, structure can definitely be better):
    - ZombieBase has Health, NavMeshAgent and MeleeAttack
    - Additional components may be: SpitPoison, DrainHealth, SpeedAura etc.

    1. Instantiate ZombieBase
    2. Add additional components you need
    3. Modify parameters if needed

    So in essence you can replicate the way you build prefabs with code if you need more Actors. Do note that for a lot of scenarios this will not be needed and can cause additional processing time where simple prefabs would suffice. But if you have a lot of possible components it might be worth it if handled responsibly.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139

    Yep, my other suggestion up top. This is more in depth and still a good idea. But, I guess depends on what is being created in this for loop. Honestly, it may be better just to make multiple prefabs with the components you need/want on them instead of disabling, adding, removing components as suggested here.
     
  9. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    You can also make a base enemy prefab that can spawn with a number of "submodule" prefabs/scriptableObjects as children/composites.

    for example if you have procedural enemy generation you can spawn the base enemy and then randomly choose a "movement module" prefab for the enemy, spawn that and attach it as a child gameobject. or with scriptableobjects have an enemy with a MovementBehaviour script that randomly chooses, for example, a ScriptableObject of type MovementDelegator. to which the movementbehaviour then delegates the actual movement to the scriptable object

    thus when an enemy spawns he'll randomly be able to move normally, or like a leaping frog, or can fly, or through walls like a ghost.

    with this implementation the MovementBehaviour script would store all the instance-specific data (like current speed, destination) while the scriptable object handles the "how it moves" logic that all the enemies will use.
     
  10. Andenberg

    Andenberg

    Joined:
    Nov 5, 2016
    Posts:
    11
    The code below should do this however it is pseudocode and I haven't tested it so there might be a syntax error hiding in there but this is how you check for and then disable a component if it exists.

    Code (CSharp):
    1. //Do I (the gameObject) have a ExampleType component attached.
    2. If(gameObject.GetComponent<ExampleType>())
    3. {
    4.     //If I do disable it.
    5.     gameObject.GetComponent<ExampleType>().enabled = false;
    6. }
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    While I agree this smacks of bad structure, you can always use GetComponents to iterate through all the available components.