Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2010
    Posts
    3

    Unhappy How can I access objects or variables from another class in C#

    Hi..

    I have two C# script called: player.cs and enemy.cs . In my enemy.cs script file I have boolean called "attack". So I want to access that from my player script.

    I tried in the following way, but I got error : NullReferenceException: Object reference not set to an instance of an object
    player.Update () (at Assets/scripts/player.cs:44)




    Ok here is my script, please tell me what I was wrong??

    player.cs:
    ------------
    Code:
    public class player : MonoBehaviour
    {
     private enemy enemyAI;
    
    public void Awake()
        {
            enemyAI = GetComponent<enemy>();
        }
    
    
    if(enemyAI.attack==true)
    {
    // do something
    }
    
    }

  2. #2
    Join Date
    Dec 2007
    Location
    Austin, Texas
    Posts
    499
    first off, I have to ask, is the boolean a public boolean?

    Code:
    public bool attack = false;
    change it to that, if it isn't, and you should be able to get access to it.

  3. #3
    Join Date
    Nov 2008
    Location
    41° 28′ 56″ N, 81° 40′ 11″ W
    Posts
    2,399
    Wouldn't that bool also have to be static as well? I know that in some cases like that, the code can be rather picky about how bools are defined sometimes, since I'm assuming you only use one attack method in the whole thing, correct?
    PC Specs: Antec 900 ATX Case, Gigabyte 790GX Mobo, AMD Phenom II x4 955, Corsair HX620W PSU, Corsair Vengeance 16GB (4x4GB) DDR3-1600, EVGA GTX 680 2GB GDDR5, 3D Vision Kit 2, LG 8x Blu-ray Burner, Intel X25-M SSD, 500GB+640GB WD Caviar Blacks, Win 7 Pro x64, DAS Pro K/B, MX518 Mouse, Logitech Z-2300 Speakers, Toshiba 32TL515U 32" 3DTV, Wacom Bamboo Fun Large Tablet.

  4. #4
    Join Date
    Dec 2007
    Location
    Austin, Texas
    Posts
    499
    No, because he is getting a reference to the Enemy script, he just needs to make sure that the bool is public.

  5. #5
    Join Date
    Sep 2008
    Location
    Salisbury, UK
    Posts
    2,812
    Firstly, welcome to the site

    Now; this isn't so much an answer as something that occurred to me whilst looking at your code snippet but, given that it's the enemy that is doing the attacking, I'd be inclined to put the logic in the enemy.cs rather than the player.cs - and accessing things like player.Health and suchlike from the enemy class, rather than vice versa...

    Although, without knowing what kind of something the something in //do something is, I can't tell whether I'm wrong about that, though

    Cheers, G.
    Last edited by fatgav; 12-31-2010 at 11:58 PM. Reason: Clarification

    I found
    my current avatar on google, so props to THIS GUY who created the original...

  6. #6
    Join Date
    Mar 2005
    Location
    Estonia
    Posts
    469
    It may be not what you are looking for but i would do it like this.

    First off. I have Player class and Enemy class. So now i want to instantiate enemy in my player class. I attach my Enemy script on a enemy gameobject that has a name "enemyGameObjectName". Then i open my Player class and instantiate the enemy object


    Code:
    Enemy enemyAI= (Enemy)GameObject.Find("enemyGameObjectName").GetComponent("Enemy");
    So now i can access all public variable in Enemy class trough enemyAI variable.

    So now all i have to do is write this line in my Player class Update() function

    Code:
    Update()
    {
         if(enemyAI.attack==true)
       {
       // do something
       }
    }
    Hope that helps.

  7. #7
    Join Date
    Dec 2010
    Posts
    3
    I got it.... Thank-You soooooo much guys.... thanks for the great answers.... HaPPY nEW Year for all ......!!!

  8. #8
    Join Date
    Apr 2003
    Posts
    485
    Quote Originally Posted by Gotmilk View Post
    Code:
    Enemy enemyAI= (Enemy)GameObject.Find("enemyGameObjectName").GetComponent("Enemy");
    I have actually found this method to often be problematic. If the GameObject requested is not found in the scene, the script will attempt to run GetComponent on null and this will throw an error.T he only time to use this is if you are 100% guaranteed this function will only get called if an enemy is in the scene. I prefer to separate the Find / GetComponent and put in some error checking, which uses a second variable but is less prone to errors.

    Code:
    GameObject enemyObj = (GameObject)GameObject.Find("enemyGameObjectName");
    if (enemyObj != null) 
       Enemy enemyAI= (Enemy)enemyObj.GetComponent("Enemy");

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •