Search Unity

code problem

Discussion in 'Scripting' started by maximmolek785439, Jan 13, 2022.

  1. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    hi i need help why does this script not work for me ?(I want it to do this when it touches these 4 things so an object is created) Thank you in advance for your answer

    Code (CSharp):
    1.  void OnTriggerStay(Collider other)
    2.     {
    3.         if (other.gameObject.tag == "Wood" & other.gameObject.tag == "Cloth" & other.gameObject.tag == "Resin" & other.gameObject.tag== "hammer")
    4.         {
    5.             Instantiate(prefab, spawn.position, spawn.rotation);
    6.         }
    7.         else
    8.         {
    9.  
    10.         }    
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,638
    Just so you know, AND in C# is &&, not &. AND is not what you want in this case, though. You probably meant OR, which is ||.
     
    Bunny83 and alexeu like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    In the above code, any give GameObject will NEVER be all of those tags at once.

    You will need to implement some type of persistent "I am touching X" so that you have another script decide "Am I touching all items necessary?"

    Ideally, start with a tutorial for a crafting game that does this type of thing. It's WAY too complex to type out all the necessary steps here.
     
  4. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    thanks for the answers but how to do it? some code would help me need an example
     
  5. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Code (CSharp):
    1. if (
    2. other.gameObject.tag == "Wood" ||
    3. other.gameObject.tag == "Cloth" ||
    4. other.gameObject.tag == "Resin" ||
    5. other.gameObject.tag == "hammer")
    6.     {
    7.         Instantiate(prefab, spawn.position, spawn.rotation);
    8.     }
    9.     else
    10.     {
    11.     }    
     
  6. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    thank you for the effort :)but it doesn't work:(, it creates an infinity of objects in the game
     
  7. maximmolek785439

    maximmolek785439

    Joined:
    Sep 19, 2020
    Posts:
    89
    I changed it to triggerenter, it is no longer an unlimited object, but when I place the first object out of four, a new one is created why ?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    I wrote:

    It will be several different scripts set up precisely just so: one to track the items, one to sense the items, one to trigger the crafting when conditions are appropriate.