How to add colliders to scripts at runtime?

Estigio97

New member
Hello, I have been implementing a weapon exchange system by instantiating them at runtime. I manage to relate the parameters that the controller needs so that my sword (in my case) works correctly. But, when I try to add the sword colliders to the colliders list of the ThirdPersonMeleeWeaponProperties script, I create an array of MeleeHitbox and try to assign the collider in its property, and then add this array to the corresponding variable of the script, but being a property read-only, I cannot overwrite or change anything of that property. How can I add these colliders at runtime to this array?

These gives me an error:
NullReferenceException: Object reference not set to an instance of an object
ACATIST.CharacterWindow.PrepareHitboxes (UnityEngine.GameObject weaponGO) (at Assets/Scripts/CharacterWindow.cs:254)
ACATIST.CharacterWindow+<SetWeapon>d__17.MoveNext () (at Assets/Scripts/CharacterWindow.cs:309)
--- End of stack trace from previous location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) (at <437ba245d8404784b9fbab9b439ac908>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <480508088aee40cab70818ff164a29d5>:0)
UnityEngine.UnitySynchronizationContext:ExecuteTasks()

HowToAddColliders01.jpg
 
The ThirdPersonMeleeWeaponProperties has a public getter property so you can assign those at runtime. After you set the hitbox make sure you initialize it to the character transform.
 
Top