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

Bool or int?

Discussion in 'Scripting' started by kittik, Nov 6, 2015.

  1. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Some objects with some code I have made will have waypoints attached to themselves and some without. Those with waypoints should interact differently to those with, and to access these different functions, I hoped that the following if statement could work:

    Code (CSharp):
    1. if (waypoints != null)
    2. {
    3. //Do stuff
    4. }
    5.  
    The above does not work for my needs, as if a prefab has 0 waypoints set, they still technically aren't null. Everything believes it has waypoints. To get around that, I tried the below:

    Code (CSharp):
    1. if (waypoints.GetLength(0))
    2. {
    3. //Do stuff
    4. }
    5.  
    Again, the above has not worked, as I am pulling an int where a bool is expected.

    I tried to change the if statement, to look for whether a prefab has 0 waypoints, but trying to set an int where a bool is expected has caused even more problems.

    I would appreciate some help on how to stop objects with 0 waypoints being able to call this, as it is a bool requirement and not an int requirement, I am not sure how to go about this.

    Thanks.
     
  2. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    If I understood this sentence correctly, you typed something like:
    Code (CSharp):
    1. int numberOfWaypoints = ... //acquire number of waypoints
    2.  
    3. if (numberOfWaypoints = 0) // <-- missing == ?
    4. {
    5.      //Do stuff
    6. }
    If you just need to count how many Waypoint components a gameObject has, simply use:
    Code (CSharp):
    1. int count = gameObject.GetComponents<Waypoint>().Length;
    2. if (count == 0)
    3.      return;
    4.  
    5. //Do stuff
    If this doesn't help, providing your Waypoint code might lead this forum post into right direction.
     
    kittik likes this.
  3. Matt-Roper

    Matt-Roper

    <Of The Graphics> Unity Technologies

    Joined:
    Oct 27, 2015
    Posts:
    106
    If waypoints is an array you can use:

    If(waypoints.Length > x) { }

    Reply with some more info if this doesn't help; i'm currently in an airport on my phone so can't help that much :)
     
    kittik likes this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    An array's GetLength method gets the length in the specified dimension. So if you create a two-dimensional array:

    Code (csharp):
    1. string[,] arr = new string[5,7];
    2. print(arr.GetLength(0)); //prints 5
    3. print(arr.GetLength(1)); //prints 7
    4. print(arr.GetLength(2)); //error
    @Matt Roper's reply is probably the correct one. You could replace .Length with .GetLength(0) as they're equivalent, but it's really only common to use the GetLength method of getting an array's length if the array has more than one dimension.
     
    kittik likes this.
  5. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Thanks for all of the feedback.

    Code (CSharp):
    1. if (waypoints.Length = 0)
    2. {
    3. //Do stuff
    4. }
    Is the solution. Really appreciate the help.
     
  6. Spoke44

    Spoke44

    Joined:
    Feb 16, 2015
    Posts:
    11
    Nope, the solution is :
    Code (CSharp):
    1. if (waypoints.Length == 0) // missing ==
    2. {
    3.     //Do stuff
    4. }
     
    Matt-Roper and kittik like this.