#Im probably an idiot but that ok

1 messages · Page 1 of 1 (latest)

raw horizon
#

can we bring this back to OnTriggerEnter then?
What exactly is other referring to? I think when I understand that I'll feel a bit more confident in continuing myself. I thought It referred to the key and the key gets destroyed.

old tapir
#

The difference between OnTriggerEnter and OnCollisionEnter is whether it interacts with Triggers or non-triggers. other is the object you collide with. Your issue is your if statements are either missing braces, have semicolons, or are just in the wrong places

#

The if statement in 1 has no {} so it only contains the next line.
The if statement in 2 ends in ; meaning it contains no lines and does nothing whatsoever
The if statement in 3 is checking a variable that is only true for one frame inside a function that is only called for one frame

raw horizon
#

so believe it or not this is the code my instructor gave me for a door and key system, nice to know that if I have anything set too trigger in my game, it gets deleted.

#

youtube best teacher...

old tapir
#

I don't think you got exactly this code

#

Your if statements are very malformed. I doubt any instructor or video would have done this

raw horizon
#

I'm not really looking at the void OnCollisionEnter section anymore since I know I have problems in OnTriggerEnter section that need to be sorted first.

#

I'm going back over my class files to see if there's something I missed read, but here's the code from the class

using System.Collections;

public class KeyCollector : MonoBehaviour
{
 // Store the number of keys.
 public int keys = 0;

 // Fires when a trigger is touched.
 // The trigger touched will be stored as "other."
 void OnTriggerEnter(Collider other)
 {
  // If the touched trigger's tag is "Key."
  if(other.gameObject.tag == "Key")
  {
   // Increase keys variable.
   keys += 1;

   // Destroy the key trigger.
   Destroy(other.gameObject);
  }
 }

 // Fires when a non-trigger collider is touched.
 // Info about the collision, including what was touched, will be stored as "collision."
 void OnCollisionEnter(Collision collisionInfo)
 {
  // Checks the collision info for the GameObject that was touched.
  // Checks if its name is "Door."
  if(collisionInfo.gameObject.tag== "Door")
  {
    //Checks to see if the player has any keys before opening the door
    if (keys > 0)
    {
     // Decrease keys variable.
     keys -= 1;

     // Destroy the door.
     Destroy(collisionInfo.gameObject);
    }
  }
 }
}```
old tapir
#

Yes, they seem to have all their if statements in order

#

They're using curly braces correctly

raw horizon
#

I'm just dense, I back up my code to ```cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class housekey : MonoBehaviour
{

public int aptkey = 0;


void OnTriggerEnter(Collider other)
{

    if (other.gameObject.tag == "hkey")
    {
       aptkey += 1;

       Destroy(other.gameObject);
    }
    
}

}

It still deletes everything checked is trigger, but when i create a new scene it only deletes things tagged `hkey` as it should. Is there some setting I might have clicked?