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

Question Scriptable objects ? SOLVED

Discussion in 'Scripting' started by maximmolek785439, Aug 10, 2023.

  1. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    Hi, I want to ask how to is updates scriptable object variables in the editor? Yes or no? because I don't know how to test if it's correct, thank you


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. namespace MainSystemGame.Craft.Items
    5. {
    6.  
    7.  
    8. [CreateAssetMenu(fileName = "MaxMaterils", menuName = "Main/MaxMaterilsInfo")]
    9. [System.Serializable]
    10.  
    11. public class MaxMaterials : ScriptableObject
    12. {
    13.  
    14.     [SerializeField]public int IronScrap;
    15.     [SerializeField]public int RepairTools;
    16.     [SerializeField]public int Wood;
    17.     [SerializeField]public int GunPowder;
    18.     [SerializeField]public int Brass;
    19.     [SerializeField]public int Chemicals;
    20.     [SerializeField]public int Bolts;
    21.     [SerializeField]public int Nails;
    22.     [SerializeField]public int Lead;
    23.     [SerializeField]public int Paper;
    24.     [SerializeField]public int Cloth;
    25.     [SerializeField]public int Leather;
    26.     [SerializeField]public float Coins;
    27. }
    28. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. namespace MainSystemGame.Craft.Items
    5. {
    6.    
    7.     public class ScrapBin : MonoBehaviour
    8.     {
    9.         public Collider col;
    10.         public MaxMaterials maxMaterials;
    11.  
    12.      
    13.         public void OnTriggerEnter(Collider col)
    14.         {
    15.             if (col.gameObject.GetComponent<HolderDestroy>())
    16.             {
    17.  
    18.                 maxMaterials.IronScrap += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.IronScrap;
    19.                 maxMaterials.Brass += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.Brass;
    20.                 maxMaterials.Wood += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.Wood;
    21.                 maxMaterials.Bolts += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.Bolts;
    22.                 maxMaterials.Chemicals += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.Chemicals;
    23.                 maxMaterials.Coins += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.Coins;
    24.                 maxMaterials.GunPowder += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.GunPowder;
    25.                 maxMaterials.Cloth += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.Cloth;
    26.                 maxMaterials.Paper += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.Paper;
    27.                 maxMaterials.Nails += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.Nails;
    28.                 maxMaterials.Lead += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.Lead;
    29.                 maxMaterials.Leather += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.Leather;
    30.                 maxMaterials.RepairTools += col.gameObject.GetComponent<HolderDestroy>().DestroyNeed.RepairTools;
    31.                 Destroy(gameObject, 0.1f);
    32.             }
    33.         }
    34.  
    35.     }
    36. }
     
  2. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    Here photo not show in editor
     

    Attached Files:

  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    My eyes...

    That is an awful lot of "GetComponents()", I would suggest caching the "HolderDestroy" since you plan on using it multiple times. The less you can use "GetComponent<>()" the better. :)
     
    Chubzdoomer likes this.
  4. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    Thank you , i try it :D
     
  5. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    but I need to know if scriptable objects do not change variables not changed, why?
     
  6. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Sorry, I'm not familiar with scriptable objects, so I have no advice in relation to how they operate.

    But since I see no "creation" of said script, I will assume you set it up in the inspector. So if it exists, it shouldn't have any issues with accepting the proper integer. So I would test:
    Code (CSharp):
    1. maxMaterials.IronScrap += 5;
    2. maxMaterials.Brass += 17;
    first, just to make sure that side of the argument works. If those values change, then you know your problem is actually in the:
    .DestroyNeed.IronScrap;
    part. And for whatever reason that function doesn't work as expected, and needs fixed.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Changes to scriptable object assets will persist between editor play mode sessions. They're often not appropriate for this sort of mutable stuff, which should instead operate within the space of a runtime scene.

    Use scriptable objects for immutable data sources, and components for the dynamic stuff.
     
  8. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    I tried it this way and it works, but the other one doesn't work, I don't know why
    Code (CSharp):
    1. maxMaterials.IronScrap += gameObject.GetComponent<HolderDestroy>().DestroyNeed.IronScrap;
    I don't know where I'm making a mistake, it just doesn't work in on trigger enter
     
  9. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    i making scrap bin throw item with holder destroy and destroy item and grab from holder destroy variables but not work
     
  10. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I would need to see the code for this/these functions. I have no clue how you set them up.
     
  11. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    ok i send here third code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. namespace MainSystemGame.Craft.Items
    5. {
    6. [System.Serializable]
    7. public class HolderDestroy : MonoBehaviour
    8. {
    9.     [SerializeField]public DestroyItems DestroyNeed;//for blueprint
    10. }
    11. }
     
  12. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    All this shows is you have a declaration for a class called "DestroyItems".. But it doesn't show that it ever gets amounted to anything.

    Does any other code tell that to make an instance of "DestroyItems.cs"? Or is set within inspector?

    Because if it's just reading a blank/default instance of "DestroyItem" class, each of it's values will be 0, or whatever the default value of that classes variables are.

    To quick test, paste this in:
    Code (CSharp):
    1. print($"iron shows {gameObject.GetComponent<HolderDestroy>().DestroyNeed.IronScrap;}");
    So if it prints out in the console:
    iron shows 0


    You know that the class of "DestroyItems" within the class of "HolderDestroy" is doing nothing, because it has nothing set.
     
  13. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    ok,nice idea, i try it and write after
     
  14. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    here photo ,works show 2 iron scrap ,so there must be an error in that in this
    Code (CSharp):
    1. maxMaterials.IronScrap += gameObject.GetComponent<HolderDestroy>().DestroyNeed.IronScrap;
     

    Attached Files:

  15. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    here
     

    Attached Files:

  16. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    No. If that "GetComponent()" call is actually giving you a value of "2"?

    And you said the call before it
    Also worked?

    Then I'll be a June bug in December... lol

    The only other thing I'm thinking, is maybe you have the other values as floats? And for some reason is giving 0. Two things to try is:
    Code (CSharp):
    1. public void OnTriggerEnter(Collider col)
    2. {
    3.     if (col.GetComponent<HolderDestroy>())
    4.     {
    5.         HolderDestroy hold = col.GetComponent<HolderDestroy>();
    6.         maxMaterials.IronScrap += (int)hold.DestroyNeed.IronScrap;
    7.         // or get the number and re-use it
    8.         int iron = hold.DestroyNeed.IronScrap;
    9.         print($"iron is {iron}");
    10.         maxMaterials.IronScrap += iron;
    11.     }
    12. }
    As first test is just see if forcing an "int" works. Then second part is out-of-the-way testing just to make sure it is coming through as an "int".
     
  17. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    Speaking of unnecessary things: you don't need to
    SerializeField
    public fields. Unity will automatically serialize public fields if it's able to. That attribute is intended for private ones.
     
    wideeyenow_unity likes this.
  18. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    hello, it works now, I figured it out, it's because there was no collider for those things, only rigidbody collider was in as child, it works now,i am sorry but thank you everyone :)
     
    wideeyenow_unity likes this.