If statement and classes

Hey i was wondering if i can use a class, in a if statement?

if you can, how to get classes from other script to another?

And how does classes works, can i put everything in a class?

-Morten Syhler

Sorry for bad english :slight_smile:

Hi Syhler, I’m not really sure what you’re asking. What are you trying to do, exactly?

Like this code, and under IF statement, can i use a class? like;

if(“classname”)

This code is just an example, i want to make a deathcount, where i use if statement :slight_smile:

IEnumerator OnCollisionEnter(Collision other)
    {
        if(other.transform.tag == "Enemy")
        {
            Instantiate (Death, transform.position, Quaternion.identity);
            _rend.enabled = false;
            yield return new WaitForSeconds(0.15f);           
            //this.transform.position = Respawn.transform.position;
            transform.position = _spawn;
            _rend.enabled = true;
        }
    }

Ah I think I understand… sorta,

From my limited understanding, you can, but you need to use a REFERENCE of an INSTANCE. Let me re-work your code post to show you:

    IEnumerator OnCollisionEnter(Collision other)
{
   if(other.transform.tag == "Enemy")
   {
      // referencing a class script on the other object so set off smaller explosions
      EnemyExplosionScript enemyExplosion = other.GetComponent<EnemyExplosionScript>();
      enemyExplosion.Boom();
      Instantiate (Death, transform.position, Quaternion.identity);
      _rend.enabled = false;
      yield return new WaitForSeconds(0.15f);   
      //this.transform.position = Respawn.transform.position;
      transform.position = _spawn;
      _rend.enabled = true;
      }
}

These two lines will allow you to get a reference of the script class called “EnemyExplosionScript” that’s on the enemy player and then cause it to explode.

To make sure that you don’t get an error (because the script isn’t there in the first place) you can try this test if

// referencing a class script on the other object so set off smaller explosions
EnemyExplosionScript enemyExplosion = other.GetComponent<EnemyExplosionScript>();
if(enemyExplosion)
{        
              enemyExplosion.Boom();
}

As for the death count you mentioned, is this the player keeping score? Or the enemy losing lives?

well not what i meant, i meant if you could put a class into the start of a if statement

like this

if(classname)
{

}

@Syhler , you’re still not explaining yourself. The answer is no, you can’t do that, but the question remains: what would you be trying to accomplish by doing that? What is it you imagine that might do?

If I were your commanding officer, and I told you: “Fire your gun only if banana!” …What would that mean to you? (It doesn’t make much sense to me.)

1 Like

oh okay, i just wanted to make a deathcounter, by making a class, and under that class should there be a code like this:

if(Class)
{
when player respawned, add one count to the deathcount
}

i know this isnt a real code, but i have more files where the player respawning, by collider on an enemy, or falling out of the world, stuff like that :slight_smile: and my idea was to create a class within all these files. which i could call from my if statement.

I’m sorry, I’m still not understanding you. What does the “if” part represent, in plain English? Do you mean “if the player has died”? Or “if the project includes this class”? Or “if the current GameObject has such a component”? Or something else?

I’m just going to take a wild guess and suppose it’s the last one: you’re really asking, is there some way to tell whether a GameObject has a certain component on it? And the answer is yes.

DeathCounter counter = someObj.GetComponent<DeathCounter>();
if (counter != null) {
    counter.IncrementCount();
}

GetComponent finds a component of the given type on the object, and returns null if there is no such component. So, this is how you check for and use a component (i.e. class that derives from MonoBehaviour) on an object.

1 Like

well that fair enough, i will figure it out :slight_smile:

How do my if statement get IEnumerator OnTriggerEnter(Collider other)?

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Teleporting : MonoBehaviour
{

  
  
    IEnumerator OnTriggerEnter(Collider other)
    {
            yield return new WaitForSeconds(2);
            SceneManager.LoadScene("Levels");
            yield return "true";
    }
  

    void Update ()
    {
      
    if(== true);
        {
          
        }  
    }

}

what you are probably asking is how you can make method bool type object?

bool MyStatementEvent()
{
    if (condition)
    return true;
   else return false;
}

if (MyStatementEvent() == true)
{
   //  some stuff
}

But i am not 100% sure what your asking thought… You can replace if with Polymorphism but i don’t think that is what your asking…

If you can also check if OnLevelWasLoaded(index) is true

How can i be so hard to understand :confused:

all i want is to read the return from IEnumerator, though an if statement like show in my code.

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Teleporting : MonoBehaviour
{
   
    IEnumerator OnTriggerEnter (Collider´other)
    {
        yield return new WaitForSeconds(2);
        SceneManager.LoadScene("Levels");
        yield return "true";
    }
   


    void Update ()
    {
       
    if(== true);
        {
           
        }   
    }

}

when you are loading a SceneManager.LoadScene(“Levels”); the only thing that will return is OnLevelWasLoaded(index), and how this is related to collision i wonder.

Maybe if you explain what you want to achieve we can help you better.

 IEnumerator OnTriggerEnter(Collider collider)
{
     collided = true;
     yield return new WaitForSeconds(2); 
     if (collided) {
         // something like this?
     }
}

This was just to try :slight_smile: but i want to lock level 2 until you have completed level 1, etc.
and i want to make a “deathcounter” that count each time you have respawned. i dont kill my gameobject, i just respawning it.

void  OnTriggerEnter(Collider  hit)
{
      if (deathcounter == 0)
      StartCoroutine(StartNextLeve("Levelname");
}
IEnumerator StartNextLevel(string name)
{
    yield return new WaitForSeconds(2);
    SceneManager.LoadScene(name);
}
1 Like

Sorry that does me more confused, no clue what I should use that for

Then sorry i can not help you…

It’s okay, thanks for trying :smiling_face:

I think you should (re)watch some c# tutorials.

2 Likes