Search Unity

Issue with Assigning Materials Repeatedly

Discussion in 'Scripting' started by Joewa654321_, Oct 7, 2021.

  1. Joewa654321_

    Joewa654321_

    Joined:
    Apr 27, 2020
    Posts:
    32
    Hi all,

    I am stuck on an issue regarding assigning materials to custom objects within my game. The context is that I have created a custom object design system to accompany certain items in my games that allows players to customize various characteristics such as sword blades, handles, materials, etc. for each item. While the spawning system works, I have encountered a strange issue with my method of creating these objects which is through an object design interface which allows the player to scroll through the selection of item characteristics via OnClick events while the custom object is represented as a gameObject being observed by a camera which loads the image as a render texture that represents the currently selected configuration for the object. These OnClick events alter integers representing characteristics and call databases containing the relevant objects and materials.

    Currently, I am able to scroll through different objects, but the materials are only assigned on the first click through, afterwards the materials are always default even though the correct material ID is being called and assigned on the object. Outside of the design interface, there are no issues with assigning materials. I have included the code pertaining to the control of characteristics:

    Code (CSharp):
    1.      
    2. int bladeID = 0;
    3. int bladeMaterialID = 0;
    4. int hiltID = 0;
    5. int hiltMaterialID = 0;
    6. int gripID = 0;
    7. int gripMaterialID = 0;
    8. int pommelID = 0;
    9. int pommelMaterialID = 0;
    10.        
    11. Transform _bladeItem = Instantiate(designerSelectionItem, objectDesigner).transform;
    12.              
    13. _bladeItem.position = new Vector3(designerSelection.position.x, designerSelection.position.y - (Screen.height * 0.06f * 0) + (Screen.height * 0.2f), designerSelection.position.z);
    14.              
    15. _bladeItem.GetComponentInChildren<TextMeshProUGUI>().text = "Blade Design";
    16.              
    17. Transform _bladeMaterial = Instantiate(designerSelectionItem, objectDesigner).transform;
    18.              
    19. _bladeMaterial.position = new Vector3(designerSelection.position.x, designerSelection.position.y - (Screen.height * 0.06f * 1) + (Screen.height * 0.2f), designerSelection.position.z);
    20.              
    21. _bladeMaterial.GetComponentInChildren<TextMeshProUGUI>().text = "Blade Material";
    22.              
    23. AddEvent(_bladeItem.GetChild(2).gameObject, EventTriggerType.PointerDown, delegate { bladeID = Mathf.Clamp(bladeID - 1, 0, CustomObjectType.GetValues(typeof(Blades)).Length); customDisplayObject.GetComponent<CustomSwordObject>().ResetData(); customDisplayObject.GetComponent<CustomSwordObject>().AssignSwordData((Blades)bladeID, (Hilts)hiltID, (Grips)gripID, (Pommels)pommelID, (HardMetals)bladeMaterialID, (HardMetals)hiltMaterialID, (HardMetals)pommelMaterialID, (GripMaterials)gripMaterialID, true); });
    24.              
    25. AddEvent(_bladeItem.GetChild(3).gameObject, EventTriggerType.PointerDown, delegate { bladeID = Mathf.Clamp(bladeID + 1, 0, CustomObjectType.GetValues(typeof(Blades)).Length); customDisplayObject.GetComponent<CustomSwordObject>().ResetData(); customDisplayObject.GetComponent<CustomSwordObject>().AssignSwordData((Blades)bladeID, (Hilts)hiltID, (Grips)gripID, (Pommels)pommelID, (HardMetals)bladeMaterialID, (HardMetals)hiltMaterialID, (HardMetals)pommelMaterialID, (GripMaterials)gripMaterialID, true); });
    26.              
    27. AddEvent(_bladeMaterial.GetChild(2).gameObject, EventTriggerType.PointerDown, delegate { bladeMaterialID = Mathf.Clamp(bladeMaterialID - 1, 0, CustomObjectType.GetValues(typeof(HardMetals)).Length); customDisplayObject.GetComponent<CustomSwordObject>().ResetData(); customDisplayObject.GetComponent<CustomSwordObject>().AssignSwordData((Blades)bladeID, (Hilts)hiltID, (Grips)gripID, (Pommels)pommelID, (HardMetals)bladeMaterialID, (HardMetals)hiltMaterialID, (HardMetals)pommelMaterialID, (GripMaterials)gripMaterialID, true); });
    28.              
    29. AddEvent(_bladeMaterial.GetChild(3).gameObject, EventTriggerType.PointerDown, delegate { bladeMaterialID = Mathf.Clamp(bladeMaterialID + 1, 0, CustomObjectType.GetValues(typeof(HardMetals)).Length); customDisplayObject.GetComponent<CustomSwordObject>().ResetData(); customDisplayObject.GetComponent<CustomSwordObject>().AssignSwordData((Blades)bladeID, (Hilts)hiltID, (Grips)gripID, (Pommels)pommelID, (HardMetals)bladeMaterialID, (HardMetals)hiltMaterialID, (HardMetals)pommelMaterialID, (GripMaterials)gripMaterialID, true); });
    30.  
    The above code involves the assignment of OnClick events to the buttons attached to recently instantiated selection objects, these are just some (ie. blade & hilt). What I cannot figure out is that my method of assignment of materials/objects works with the objects in every other situation where I send integer values that are read as an enum value for each of the characteristics (ie. spawning the item on the ground or in the players hand), but for whatever reason the materials fail to assign after the first scroll through when using my design interface. Any suggestions? I will post more code if necessary.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Are you sure your values are changing after your callbacks? I'm wondering if your anonymous delegate is capturing the original state of the integer and thus why it's the only value "that's working".
     
  3. Joewa654321_

    Joewa654321_

    Joined:
    Apr 27, 2020
    Posts:
    32
    Thank you for your reply! It does seem that my values are changing and retaining the value as the object does change as well as the enumerator representing the material on the object with each selection. I think you are on the right track as the system works in every other instance/on the first selection so it makes sense that something is not being assigned correctly, just need to figure out where my mistake is. This has given me something to look for, will keep trying
     
    Last edited: Oct 7, 2021
  4. Joewa654321_

    Joewa654321_

    Joined:
    Apr 27, 2020
    Posts:
    32
    Just wanted to follow up, I ended up solving it by simply re-instantiating the base object with every OnClick event. Your comment helped me figure it out, so thank you for the assistance
     
    GroZZleR likes this.