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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I don't understand how this is not working.

Discussion in 'Scripting' started by Calick, Feb 23, 2020.

  1. Calick

    Calick

    Joined:
    Sep 2, 2018
    Posts:
    4
    Hi! I'm attempting to make a crafting system where (for short answer) when something is able to craft the variable "craftable" becomes true, and when the crafting button is pressed it becomes false. However for some reason you have to press the button twice for craftable to become false, giving you 2 items instead of 1. I honestly have no clue.

    This is script crafting:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Crafting : MonoBehaviour
    6. {
    7.  
    8.     public bool craftable;
    9.     private Inventory inventory;
    10.     public GameObject crafted;
    11.     public GameObject crafting1;
    12.     public GameObject crafting2;
    13.     public GameObject Inventory;
    14.     public GameObject slot1;
    15.     public GameObject slot2;
    16.  
    17.     public GameObject stick;
    18.     public GameObject boxOfMatches;
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         inventory = Inventory.GetComponent<Inventory>();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.  
    30.         if (GameObject.Find("StickCrafting(Clone)") != null && GameObject.Find("StickCrafting1") == null)
    31.         {
    32.             GameObject.Find("StickCrafting(Clone)").name = "StickCrafting1";
    33.         }
    34.         if (GameObject.Find("StickCrafting(Clone)") != null && GameObject.Find("StickCrafting2") == null)
    35.         {
    36.             GameObject.Find("StickCrafting(Clone)").name = "StickCrafting2";
    37.         }
    38.  
    39.  
    40.  
    41.  
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
    48.  
    49.         if (GameObject.Find("StickCrafting1") != null && GameObject.Find("StickCrafting2") != null && craftable == false)
    50.         {
    51.                 craftable = true;
    52.                 crafting1 = GameObject.Find("StickCrafting1");
    53.                 crafting2 = GameObject.Find("StickCrafting2");
    54.                 crafted = boxOfMatches;
    55.         }
    56.  
    57.     }
    58.  
    59.     public void craft()
    60.     {
    61.         for (int i = 0; i < inventory.slots.Length; i++)
    62.         {
    63.             if (inventory.isFull == false)
    64.             {
    65.                 inventory.isFull = true;
    66.                 Instantiate(crafted, inventory.slots.transform, false);
    67.                 Destroy(crafting1);
    68.                 Destroy(crafting2);
    69.                 craftable = false;
    70.                 break;
    71.             }
    72.         }
    73.     }
    74. }

    This is the script for the button:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CraftingButton : MonoBehaviour
    7. {
    8.  
    9.     public GameObject CraftingMenu;
    10.     public Image image;
    11.     public Button button;
    12.     public SpriteRenderer sr;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.      
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if(CraftingMenu.GetComponent<Crafting>().craftable != true)
    24.         {
    25.             sr.enabled = false;
    26.             image.enabled = false;
    27.             button.enabled = false;
    28.         }
    29.         else
    30.         {
    31.             sr.enabled = true;
    32.             image.enabled = true;
    33.             button.enabled = true;
    34.         }
    35.     }
    36. }

    And this is the button component:
    CaptureButton.PNG

    Help would be greatly appreciated! Thanks in advanced!
     
    Last edited: Feb 23, 2020
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    must be error in logic check carefully
     
  3. Calick

    Calick

    Joined:
    Sep 2, 2018
    Posts:
    4
    I have tried everything and checked a lot.
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    OP - could you please edit your post to use code tags? Then the code would be more readable.
     
  5. Calick

    Calick

    Joined:
    Sep 2, 2018
    Posts:
    4
    Sorry, done.
     
  6. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,803
    Do not use Find in Update(). Find them in start or set some boolean switch in Update to call find once in a dedicated function. If you only need to find them once do it in Awake or Start.
     
    Nefera likes this.