Search Unity

How should i create & add components to a gameobject in a loop?

Discussion in 'Scripting' started by gizmo87898, Jun 1, 2021.

  1. gizmo87898

    gizmo87898

    Joined:
    Mar 11, 2021
    Posts:
    10
    Hello,
    I'm trying to add some more functionality to a script and im confused on how to use instantiate. I have an OnEntityCreated callback and a switch statement, and i need to create a gameobject & give it some components depending on what part of the switch statement it went to. Do i need to create a prefab and use that or should i create an empty gameobject and add the components like that? thanks
    Code (CSharp):
    1. void OnEntityCreated(BSPLoader.EntityInstance instance, List<BSPLoader.EntityInstance> targets) {
    2.             Debug.Log("Creating \"" + instance.entity.ClassName + "\" at \"" + instance.entity.Origin + "\" raw obj: " + instance.entity);
    3.             //GameObject model = FindModel(instance.entity.Model);
    4.             GameObject model = AssetDatabase.LoadAssetAtPath<GameObject>(instance.entity.Model);
    5.             model = AssetDatabase.LoadAssetAtPath<GameObject>(instance.entity.Model);
    6.             Instantiate(model, instance.entity.Origin, Quaternion.identity);
    7.             switch (instance.entity.ClassName)
    8.             {
    9.                 case "func_door":
    10.                    
    11.                     break;
    12.                 case "func_button":
    13.                    
    14.                     break;
    15.                 case "ambient_generic":
    16.                    
    17.                     break;
    18.                 case "prop_physics":
    19.                  
    20.                     break;
    21.                 case "prop_dynamic":
    22.                    
    23.                     break;
    24.                 case "npc_security_camera":
    25.  
    26.                    
    27.                     break;
    28.                 case "func_light":
    29.                    
    30.                     break;
    31.                 case "default":
    32.                     Debug.LogWarning("No matches for " + instance.entity.ClassName + " in the switch statement");
    33.                     break;
    34.             }
    35.          
    36.     }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Instantiate returns the object that was created, so use that. AddComponent also returns the component you just added, so you can set values to it.

    Code (csharp):
    1.  
    2. GameObject instance = Instantiate(model, instance.entity.Origin, Quaternion.identity);
    3.  
    4. // for example:
    5. switch (instance.entity.ClassName)
    6. {
    7.    case "func_light":
    8.        Light light = instance.AddComponent<Light>();
    9.        light.intensity = // however you're reading the value from the file
    10.                    
    11.        break;
    12. }
    13.  
    You might be a little in over your head though trying to mod / re-create another game or whatever you're doing here exactly.
     
    Kurt-Dekker likes this.