Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

245 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges