comparing name of gameobject to its parent name

Piyush Damania

Here is my code:

GameObject no=GameObject.Find("LevelButton");
GameObject noparent=GameObject.Find("LevelPHwithrect (" + (i + 1) + ")");

The problem is that I am able to find the gameobjects, but when I call

if(no.transform.parent.name==noparent.transform.name)
{
    //do something
}

I get an error saying null reference, even when it's finding the object and I have set parent of no to noparent.

Please forgive me if there are errors in this post, as this is my first day on stack overflow.

trojanfoe

Use the name property of the GameObject, not Transform. Also code defensively and check for null:

if (no != null && noparent != null && no.name == noparent.name)
{
    //do something
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Name of a constant given its value

From Dev

Aborting a thread via its name

From Dev

Getting the ChildNode name and its Value

From Dev

table with "." in its name

From Dev

How to get the name of the parent folder of a file specified by its full path?

From Dev

Close specific form by its name

From Dev

Get instance variable by its name

From Dev

Access Gtk3 widget from its parent by its name

From Dev

Parent directories name combined with the new directory name

From Dev

Defining and casting a class by its name

From Dev

Is there a way to select parent div without knowing its class name or ID ?

From Dev

bsearch() comparing by name

From Dev

Comparing part of a file name to a string in Java

From Dev

Adding extra functionality to parent class method without changing its name

From Dev

Is it possible to have a parent state without having its name in the URL?

From Dev

Comparing file name to string

From Dev

Serialize JSON property name with comma in its name?

From Dev

How to move files from subdirectories that have the same directory name to its relative upper/parent directory?

From Dev

Pythonic way to extract the file name and its parent directory from the full file path?

From Dev

Folder name comparing - script

From Dev

how to get all parent name and its id in a single table

From Dev

Is there a way to select parent div without knowing its class name or ID ?

From Dev

bsearch() comparing by name

From Dev

Instantiating gameObject relative to its parent Unity

From Dev

Is it possible to have a parent state without having its name in the URL?

From Dev

Select related Row without comparing Street Name

From Dev

Unity (Object name vs gameobject)

From Dev

How to get a function name using a parent name?

From Dev

comparing file and folder name in python

Related Related

  1. 1

    Name of a constant given its value

  2. 2

    Aborting a thread via its name

  3. 3

    Getting the ChildNode name and its Value

  4. 4

    table with "." in its name

  5. 5

    How to get the name of the parent folder of a file specified by its full path?

  6. 6

    Close specific form by its name

  7. 7

    Get instance variable by its name

  8. 8

    Access Gtk3 widget from its parent by its name

  9. 9

    Parent directories name combined with the new directory name

  10. 10

    Defining and casting a class by its name

  11. 11

    Is there a way to select parent div without knowing its class name or ID ?

  12. 12

    bsearch() comparing by name

  13. 13

    Comparing part of a file name to a string in Java

  14. 14

    Adding extra functionality to parent class method without changing its name

  15. 15

    Is it possible to have a parent state without having its name in the URL?

  16. 16

    Comparing file name to string

  17. 17

    Serialize JSON property name with comma in its name?

  18. 18

    How to move files from subdirectories that have the same directory name to its relative upper/parent directory?

  19. 19

    Pythonic way to extract the file name and its parent directory from the full file path?

  20. 20

    Folder name comparing - script

  21. 21

    how to get all parent name and its id in a single table

  22. 22

    Is there a way to select parent div without knowing its class name or ID ?

  23. 23

    bsearch() comparing by name

  24. 24

    Instantiating gameObject relative to its parent Unity

  25. 25

    Is it possible to have a parent state without having its name in the URL?

  26. 26

    Select related Row without comparing Street Name

  27. 27

    Unity (Object name vs gameobject)

  28. 28

    How to get a function name using a parent name?

  29. 29

    comparing file and folder name in python

HotTag

Archive