- Home /
 
               Question by 
               jebyork2000 · Apr 12 at 09:25 AM · 
                scripting problemscripting beginner  
              
 
              Im having trouble using different scriptable objects in a script
Basically ive got a building system currently with three different scriptable objects, however i plan to add more. The problem im having is that i have to use if statements for each different scriptable object and i was wondering if there is a way that i could reference the different types of scriptable object with more readable code.
 public static PlayerControls instance { get; private set; }
 /// <summary>
 /// Event to call a change to the Ui
 /// </summary>
 public static event DebugDisplayDelegate OnChangeDisplay;
 public delegate void DebugDisplayDelegate(Node node);
 /// <summary>
 /// References to the direction to place objects
 /// </summary>    
 private ShelfSO.Dir shelfDir = ShelfSO.Dir.Down;
 private WallsSO.Dir wallsDir = WallsSO.Dir.Down;
 private StorageSO.Dir storageDir = StorageSO.Dir.Down;
 [Header("components")]
 private GridGeneration gridGen;
 private void Awake() {
     instance = this;
     placeShelfSO = placedObjectTypeShelfSOList[0];
     wallsSO = wallsSOList[0];
 }
 private void Start() {
     gridGen = GridGeneration.instance;
 }
 private void Update() {
     //Check Tile Debug
     CheckTileInfo();
     if (Input.GetMouseButtonDown(0) && MousePosition.instance.Check())
     {
         gridGen.grid.GetXZ(MousePosition.instance.mouseVector3(), out int x, out int z);
         if (buildObject == 0 && placeShelfSO != null) {
             //Place Shelf
             placeShelfSO.PlaceShelf(shelfDir, x, z);
             //Change UI
             PlayerControls.OnChangeDisplay?.Invoke(GridGeneration.instance.grid.GetGridObject(x, z));
         }
         if (buildObject == 1) {
             //Place Wall
             wallsSO.PlaceWall(wallsDir, x, z);
             //change UI
             PlayerControls.OnChangeDisplay?.Invoke(GridGeneration.instance.grid.GetGridObject(x, z));
         }
         if (buildObject == 2) {
             //Place Shelf
             storageSO.PlaceStorage(storageDir, x, z);
             //Change UI
             PlayerControls.OnChangeDisplay?.Invoke(GridGeneration.instance.grid.GetGridObject(x, z));
         }
     }
     
     
     //Destroy Selected Shelf
     DestroyShelf();
     //RemoveProductFrom The Dropped Item
     RemoveProduct(GetGridInfoInThisTile());        
     //Rotates shelf SO
     if (Input.GetKeyDown(KeyCode.R)) {
         if (buildObject == 0 && placeShelfSO != null) {
             shelfDir = ShelfSO.GetNextRotation(shelfDir);
         }
         if (buildObject == 1) {
             wallsDir = WallsSO.GetNextRotation(wallsDir);
         }
         if (buildObject == 2) {
             storageDir = StorageSO.GetNextRotation(storageDir);
         }
     }
     
     //Changes the selected shelf to place
     NumberInputKeys();
     //Adds Money
     if (Input.GetKeyDown(KeyCode.P))
     {
         MoneyManagement.AddMoney(100);
     }        
 }
 //Destroys Shelf
 private void DestroyShelf() {
     if (Input.GetMouseButtonDown(2))
     {
         Node shopGridInfo = GetGridInfoInThisTile();
         Vector3 placedObjectWorldPosition = gridGen.grid.GetWorldPosition(shopGridInfo.GetIntX(), shopGridInfo.GetIntZ());
         PlacedShelfObject placedShelfObject = shopGridInfo.GetPlacedShelfObject();           
         if (placedShelfObject != null)
         {
             List<ProductsSO> productsCheck = new List<ProductsSO>();
             foreach(ProductsSO products in placedShelfObject.GetListOfProducts()) {
                 if(products != null) {
                     productsCheck.Add(products);
                     Debug.Log(products);
                 }                    
             }
             Vector2Int destroyedShelfOrigin = placedShelfObject.GetOrigin();
             Vector3 destroyedShelfWorldPos = placedShelfObject.GetWorldPos();
             
             if (productsCheck.Count > 0) {
                 DroppedProduct droppedProduct = DroppedProduct.Create(destroyedShelfWorldPos, destroyedShelfOrigin, droppedProductSO, placedShelfObject);
                 gridGen.grid.GetGridObject(destroyedShelfOrigin.x, destroyedShelfOrigin.y).ClearPlacedShelfObject(droppedProduct);                   
             } else {         
                 gridGen.grid.GetGridObject(destroyedShelfOrigin.x, destroyedShelfOrigin.y).ClearPlacedShelfObject(null);                   
             }           
           
             placedShelfObject.DestroySelf();
         }
     }
 }
 /// <summary>
 /// Returns the Node At Mouse Pos
 /// </summary>
 /// <returns>Node</returns>
 private Node GetGridInfoInThisTile() {
     gridGen.grid.GetXZ(MousePosition.instance.mouseVector3(), out int x, out int z);
     return gridGen.grid.GetGridObject(x, z);
 }
 
 /// <summary>
 /// Sets the UI to siplay info of selected GridPos
 /// </summary>
 private void CheckTileInfo() {
     if (Input.GetMouseButtonDown(1) && MousePosition.instance.Check())
     {
         gridGen.grid.GetXZ(MousePosition.instance.mouseVector3(), out int x, out int z);
         if (GetGridInfoInThisTile() != null)
         {
             //ChangeUI
             Node node = GetGridInfoInThisTile();
             OnChangeDisplay?.Invoke(GridGeneration.instance.grid.GetGridObject(x, z));
         }
     }
 }
 /// <summary>
 /// Removes a product from the dropped item GameObj currently just a debug tool
 /// </summary>
 /// <param name="removeFromThisNode">Pass in a specific Node with a dropped item object on</param>
 private void RemoveProduct(Node removeFromThisNode) {
    
     if (Input.GetKeyDown(KeyCode.R) && removeFromThisNode != null) {
         if(removeFromThisNode.GetProduct() != null) {
             DroppedProduct droppedItem = removeFromThisNode.GetProduct();
             droppedItem.TakeProduct();
         }
     }
 }
 /// <summary>
 /// This event will be called to change BuildingGhost visual transform
 /// </summary>
 public event changeGhostVisual OnChangeVisual;
 public delegate void changeGhostVisual(Transform visual);
 [Header("Spawn Object")]
 /// <summary>
 /// These are the refferences that the methods need to place objcects in the world
 /// </summary>
 [SerializeField] private List<ShelfSO> placedObjectTypeShelfSOList;
 private ShelfSO placeShelfSO;
 [SerializeField] private DroppedProductSO droppedProductSO;
 [SerializeField] private List<WallsSO> wallsSOList;
 [SerializeField] private StorageSO storageSO;
 private WallsSO wallsSO;
 
 int buildObject = 0;
 int lastBuildObject = 0; 
 /// <summary>
 /// This changes shelf object will be placed or which wall will be placed
 /// </summary>
 private void NumberInputKeys()
 {        
     //Changes Shelfbuild bool so can build walls and shelves
     if (Input.GetKeyDown(KeyCode.S))
     {
         buildObject++;
         if (buildObject > 2) {
             buildObject = 0;
         } 
     }
     if (buildObject == 0) { 
         if (lastBuildObject != 0) { placeShelfSO = placedObjectTypeShelfSOList[0]; OnChangeVisual?.Invoke(placeShelfSO.visual); lastBuildObject = 0; }
         if (Input.GetKeyDown(KeyCode.Alpha1)) { placeShelfSO = placedObjectTypeShelfSOList[0]; OnChangeVisual?.Invoke(placeShelfSO.visual); }
         if (Input.GetKeyDown(KeyCode.Alpha2)) { placeShelfSO = placedObjectTypeShelfSOList[1]; OnChangeVisual?.Invoke(placeShelfSO.visual); }
         if (Input.GetKeyDown(KeyCode.Alpha3)) { placeShelfSO = placedObjectTypeShelfSOList[2]; OnChangeVisual?.Invoke(placeShelfSO.visual); }
         if (Input.GetKeyDown(KeyCode.Alpha4)) { placeShelfSO = null; OnChangeVisual?.Invoke(null); }
     }
     if (buildObject == 1) {
         if (lastBuildObject != 1) { wallsSO = wallsSOList[0]; OnChangeVisual?.Invoke(wallsSO.visual); lastBuildObject = 1; }
         if (Input.GetKeyDown(KeyCode.Alpha1)) { wallsSO = wallsSOList[0]; OnChangeVisual?.Invoke(wallsSO.visual); }
         if (Input.GetKeyDown(KeyCode.Alpha2)) { wallsSO = wallsSOList[1]; OnChangeVisual?.Invoke(wallsSO.visual); }
     }
     if (buildObject == 2) {
         if (lastBuildObject != 2) { storageSO = storageSO; OnChangeVisual?.Invoke(storageSO.visual); lastBuildObject = 2; }
         if (Input.GetKeyDown(KeyCode.Alpha1)) { storageSO = storageSO; OnChangeVisual?.Invoke(storageSO.visual); }
     }
 }
 /// <summary>
 /// Returns the Shelf that is currently selected from the list
 /// </summary>
 /// <returns>Shelf Scriptable Object</returns>
 public ShelfSO GetCurrentShelfSO() {
     //returns Shelf
     return placeShelfSO;
 }
 /// <summary>
 /// Returns the Wall that is currently selected from the list
 /// </summary>
 /// <returns>Wall Scriptable Object</returns>
 public WallsSO GetCurrentWallsSO()
 {
     //returns Shelf
     return wallsSO;
 }
 /// <summary>
 /// Returns a Vector 3 for The Grid Positions Anchor Point
 /// </summary>
 /// <returns>Vector3</returns>
 public Vector3 GetMouseWorldSnappedPosition() {
     Vector3 mousePosition = MousePosition.instance.mouseVector3();
     gridGen.grid.GetXZ(mousePosition, out int x, out int z);
     if (buildObject == 0 && placeShelfSO != null)
     {
         Vector2Int rotationOffset = placeShelfSO.GetRotationOffset(shelfDir);
         Vector3 placedObjectWorldPosition = gridGen.grid.GetWorldPosition(x, z) + new Vector3(rotationOffset.x, 0, rotationOffset.y) * gridGen.grid.GetNodeDiameter();
         return placedObjectWorldPosition;
     } 
     if (buildObject == 1) {
         Vector2Int rotationOffset = wallsSO.GetRotationOffset(wallsDir);
         Vector3 placedObjectWorldPosition = gridGen.grid.GetWorldPosition(x, z) + new Vector3(rotationOffset.x, 0, rotationOffset.y) * gridGen.grid.GetNodeDiameter();
         return placedObjectWorldPosition;
     }
     if (buildObject == 2) {
         Vector2Int rotationOffset = storageSO.GetRotationOffset(storageDir);
         Vector3 placedObjectWorldPosition = gridGen.grid.GetWorldPosition(x, z) + new Vector3(rotationOffset.x, 0, rotationOffset.y) * gridGen.grid.GetNodeDiameter();
         return placedObjectWorldPosition;
     }
     else {
         return mousePosition;
     }
 }
 /// <summary>
 /// Gets of object abouts to be placed
 /// </summary>
 /// <returns>Quaternion</returns>
 public Quaternion GetPlacedObjectRotation() {
     if (buildObject == 0 && placeShelfSO != null) {
         return Quaternion.Euler(0, placeShelfSO.GetRotationAngle(shelfDir), 0);
     }
     if (buildObject == 1) {
         return Quaternion.Euler(0, wallsSO.GetRotationAngle(wallsDir), 0);
     }
     if (buildObject == 2) {
         return Quaternion.Euler(0, storageSO.GetRotationAngle(storageDir), 0);
     }
     else {
         return Quaternion.identity;
     }
 }
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
OnTriggerEnter2D crashing editor 0 Answers
On/Off Trigger is making Light Flicker 1 Answer
Can't seem to get dash cooldown working... 1 Answer
My camera script is having a lot of problems..... 2 Answers
Script Help -- OnCollisionEnter2D 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                