Search Unity

Having an issue with finding the solution of this code, please help!

Discussion in '2D' started by CptBubbleButt, May 2, 2018.

  1. CptBubbleButt

    CptBubbleButt

    Joined:
    Apr 24, 2018
    Posts:
    27
    Hello people! So I have made a very basic building mechanic - like very basic, and I am trying to deduct the materials that the players collect whenever they place the building down. I know it is basic but for some reason, I cannot get it over my head. Here is the section of the script that is the issue:

    Code (CSharp):
    1. void Update()
    2.     {
    3.        
    4.         if (buildPlacement.hasPlaced == true)
    5.         {
    6.             TotalWoodAmount -= WoodCost;
    7.             TotalRockAmount -= RockCost;
    8.             SetWoodCountText();
    9.             SetRockCountText();
    10.         }
    11.  
    12.     }
    The code works but as you can tell it's in a "void Update()" function which means the subtraction happens every frame meaning I get up to -3000 by the time I ended the game. I have tried to put it into different functions but it doesn't work, this is the part I am struggling with. (also here is the script that "buildPlacement" is referencing):

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class BuildPlacement : MonoBehaviour {
    6.  
    7.     private PlaceableBuilding placeableBuilding;
    8.    
    9.  
    10.     private Transform currentBuilding;
    11.     public bool hasPlaced;
    12.  
    13.  
    14.    
    15.  
    16.    
    17.     public LayerMask buildingmask;
    18.     // Use this for initialization
    19.     void Start() {
    20.        
    21.        
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.  
    28.  
    29.        
    30.         Vector3 m = Input.mousePosition;
    31.         Vector3 p = Camera.main.ScreenToWorldPoint(m);
    32.  
    33.  
    34.         if (currentBuilding != null && !hasPlaced)
    35.         {
    36.             currentBuilding.position = new Vector3(p.x, p.y, 0);
    37.             if (Input.GetMouseButtonDown(0))
    38.             {
    39.                
    40.                
    41.                 if (IsLegalPosition())
    42.                 {
    43.                     hasPlaced = true;
    44.                    
    45.                 }
    46.             }
    47.         }
    48.    
    49.     }
    50.         bool IsLegalPosition() {
    51.         if (placeableBuilding.colliders.Count > 0)
    52.         {
    53.             return false;
    54.            
    55.         }
    56.        
    57.        
    58.             return true;
    59.        
    60.     }
    61.  
    62.     public void SetItem(GameObject b){
    63.         hasPlaced = false;
    64.         currentBuilding = ((GameObject)Instantiate(b)).transform;
    65.         placeableBuilding = currentBuilding.GetComponent<PlaceableBuilding>();
    66.      
    67.     }
    68. }
    69.  
    70.  
    If you can find the solution, then great! I will greatly appreciate it!
     
  2. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    372
    So which ways have you tried that don't work?

    Can't you just make a public function in the first example like

    Code (CSharp):
    1. public void PlaceBuilding ()
    2. {
    3.      
    4.         if (buildPlacement.hasPlaced == true)
    5.         {
    6.             TotalWoodAmount -= WoodCost;
    7.             TotalRockAmount -= RockCost;
    8.             SetWoodCountText();
    9.             SetRockCountText();
    10.         }
    11.     }


    and call it in the second script in the line where you switch the boolean? (line 43)
     
    vakabaka likes this.
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I whould do with public function too, as BambooHutGames said, still you can try to switch boolean to false
    Code (CSharp):
    1. void Update()
    2.     {
    3.      
    4.         if (buildPlacement.hasPlaced == true)
    5.         {
    6.             buildPlacement.hasPlaced = false;
    7.             TotalWoodAmount -= WoodCost;
    8.             TotalRockAmount -= RockCost;
    9.             SetWoodCountText();
    10.             SetRockCountText();
    11.         }
    12.     }
     
  4. CptBubbleButt

    CptBubbleButt

    Joined:
    Apr 24, 2018
    Posts:
    27
    So I tried putting the code into their own function, but calling the function when mean I have to call the game object in Script Two (Unless I am missing something) and it conflicts with the "void SetItem" function for some reason. Also, Vakabaka code kinda works. The cost goes down like it should be the building isn't getting placed.
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I can't see where are you executing the build funktion. May be the script missing it.
    Code (CSharp):
    1. if (IsLegalPosition())
    2.                 {
    3.                     hasPlaced = true;
    4.                     SetItem(hereShouldBeGameObject);
    5.                 }
     
  6. CptBubbleButt

    CptBubbleButt

    Joined:
    Apr 24, 2018
    Posts:
    27
    Code (CSharp):
    1.  bool IsLegalPosition() {
    2.         if (placeableBuilding.colliders.Count > 0)
    3.         {
    4.             return false;
    5.          
    6.         }
    7.      
    8.      
    9.             return true;
    10.      
    11.     }
    12.     public void SetItem(GameObject b){
    13.         hasPlaced = false;
    14.         currentBuilding = ((GameObject)Instantiate(b)).transform;
    15.         placeableBuilding = currentBuilding.GetComponent<PlaceableBuilding>();
    16.    
    17.     }
    This is the section of code for the buildings to be placed and it goes into this function:
    Code (CSharp):
    1.   if (currentBuilding != null && !hasPlaced)
    2.         {
    3.             currentBuilding.position = new Vector3(p.x, p.y, 0);
    4.             if (Input.GetMouseButtonDown(0))
    5.             {
    6.              
    7.              
    8.                 if (IsLegalPosition())
    9.                 {
    10.                     hasPlaced = true;
    11.                  
    12.                 }
    13.             }