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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Noob] Int question

Discussion in 'Scripting' started by Janix13, Mar 31, 2016.

  1. Janix13

    Janix13

    Joined:
    Oct 8, 2013
    Posts:
    4
    I've tried the 'answers' site, but never seem to get replies there so hopefully someone can help me here.

    Ok, so I have set a public int to my c# script as follows:

    Code (CSharp):
    1. public int isEnemy;
    I have changed the value on the actual object to "1".

    I have other objects which are set to '0' (not an enemy).

    I want to use this int in another script to check whether or not the object is an "enemy" based on the value i manually set.

    The method I used, as the int is not "static" - was to create another 'static' version:

    Code (CSharp):
    1. public static int yesEnemy;
    And then in a Start() function, try and set them to be the same:


    Code (CSharp):
    1.  void Start()
    2.      {
    3.        yesEnemy = isEnemy;
    4.      }
    But when I try to call "yesEnemy" from my other script to check whether it is "1" - it always returns as "0"..

    Can anyone help me figure this out please as I've been unsuccessful :(. I'm very new to all this so I'm just learning at the moment.. It's probably very obvious.

    Thanks in advance~
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Don't use static veriables.

    Watch a tutorial on GetComponent. I have one in my signature under my interactive tutorial thread. You need to get a reference to the component you want, and then access that value.
     
  3. Janix13

    Janix13

    Joined:
    Oct 8, 2013
    Posts:
    4
    Hey, thanks for the reply I appreciate it. I'll check out your tutorials now. I already use some aspects of GetComponent so hopefully will be straight forward :).
     
  4. Janix13

    Janix13

    Joined:
    Oct 8, 2013
    Posts:
    4
    I watched your GetComponent video, and while I can see how it would be useful in this scenario I was unable to implement it.

    I get (what seems to be a common issue) in: "Object reference not set to an instance of an object".

    I believe this is because there is multiple instances of the component in my project and it doesn't know specifically which one I want to pull the info from?

    i.e I have a script attached to 20 items, each with the variable: "isEnemy". 10 have that set to 1, the other 10 are set to 0. I want it to specifically pull info from the GameObject being used to check whether or not it is an enemy or not..
     
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    So you need a list of that type (component)

    Iterate through it with a foreach loop, and check each indiivual element to see if the int is a value of 1 or 0.

    Code (CSharp):
    1. list<Component> objs = new list<Component>();
    2.  
    3. foreach (var c in components)
    4. {
    5. //if c.isEnemy == 1
    6. //We have an enemy!
    7. }
     
  6. Janix13

    Janix13

    Joined:
    Oct 8, 2013
    Posts:
    4
    Awesome exactly what I needed, thanks a lot. :)
     
    SubZeroGaming likes this.
  7. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Because it said noob in the title I thought Id offer another solution also.

    Try using the tags built into unity. That way you can simply ask if a gameobject is an enemy with only a reference to to gameobject.
     
  8. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    And while your 1 or 0 values work fine, a common data type for true/false type values is the boolean type, e.g...

    Code (CSharp):
    1. bool isEnemy;
    2.  
    3. isEnemy = true;
    4.  
    5. if (isEnemy)
    6. {
    7.   Debug.Log("isEnemy = true");
    8. }
    9. else
    10. {
    11.   Debug.Log("isEnemy = false");
    12. }
    That can make your code more clear and can help with bugs by not allowing values other than true or false, whereas your int variable can have values other than 0 or 1 which could cause hard to find bugs.
     
    HAlbera likes this.