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. Dismiss Notice

how to get class name ?

Discussion in 'Scripting' started by acropole, Feb 10, 2010.

Thread Status:
Not open for further replies.
  1. acropole

    acropole

    Joined:
    Aug 13, 2009
    Posts:
    171
    Hi,
    I don't find any way to get the class type of an object. Is there any ?
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Do you actually need the class's name or do you just need to compare the types of two objects?
     
    Yoreki likes this.
  3. acropole

    acropole

    Joined:
    Aug 13, 2009
    Posts:
    171
    I need to know if an object entering a trigger is a valid object for this trigger. Only some classes should trigger it and the result depends on object type.
     
  4. Statement

    Statement

    Guest

    Code (csharp):
    1. someObject.GetType();
    See System.Object.GetType()

    Code (csharp):
    1. if (someObject is SomeType)
    2. {
    3.   // Ok so it's one of our types
    4. }
    Or perhaps you only need to do this:

    Code (csharp):
    1. if (someObject.CompareTag("Player"))
    2. {
    3.   // Ok so it is tagged "Player" from the editor
    4. }
    If you need to check if the colliding object has a certain script on it:

    Code (csharp):
    1. if (someObject.GetComponent("SomeScript") != null)
    2. {
    3.   // Ok so it has SomeScript on it.
    4. }
    Above code is C#. I doubt JS is much different, if at all.
     
  5. MagyarPeter

    MagyarPeter

    Joined:
    Nov 28, 2020
    Posts:
    10
    this.GetType().Name
    *referenceToObject.GetType().Name
    *Type.Name
    Getcomponent<*component>().GetType().Name
     
Thread Status:
Not open for further replies.