Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help with C# Script

Discussion in 'Scripting' started by myka, Sep 4, 2012.

  1. myka

    myka

    Joined:
    Jan 9, 2012
    Posts:
    71
    Hey there, I need some help with a script that I got from a Tutorial ( http://www.youtube.com/watch?v=li6ha2d8Arw&feature=plcp ). I copied it exactly as shown in the video and I still get this error,

    Assets/Character Classes/BaseStat.cs(34,13): error CS0266: Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)

    Heres The code, please no hate I only code in Javascript not C# but this person is using C# so Im just following that. Thanks for your time :D

    Code (csharp):
    1. public class BaseStat {
    2.    private int _baseValue;            //the boa volie of this stat
    3.    private int _buffValue;            //the amount of the buff to this stat
    4.    private int _expToLevel;           //the total amount of exp needed to raise this skill
    5.    private float _levelModifier;      //the modifier applied to the exp needed to raise the skill
    6.    
    7.    public BaseStat(){
    8.         _baseValue = 0;
    9.         _baseValue = 0;
    10.         _levelModifier = 1.1f;
    11.         _expToLevel = 100;
    12.     }
    13.    
    14. #region Bassic Setters and Getters
    15.     //Bassic Setters and Getters
    16.     public int BaseValue {
    17.         get{ return _baseValue;}
    18.         set{ _baseValue = value;}
    19.     }
    20.     public int BuffValue {
    21.         get{ return _baseValue;}
    22.         set{ _baseValue = value;}
    23.     }
    24.     public int ExpToLevel {
    25.         get{ return _baseValue;}
    26.         set{ _baseValue = value;}
    27.     }
    28.     public int LevelModifier {
    29.         get{ return _baseValue;}
    30.         set{ _baseValue = value;}
    31.     }
    32.     #endregion
    33.     private int CalculateExpToLevel() {
    34.         return _expToLevel * _levelModifier;
    35.     }
    36.    
    37.     public void LevelUp() {
    38.         _expToLevel = CalculateExpToLevel();
    39.         _baseValue++;
    40.     }
    41.    
    42.     public int AdjustedValue() {
    43.         return _baseValue;
    44.     }
    45. }  
    46.  
    47.  
    48.  
     
  2. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    C# will warn you when casting from any numerical type to any other numerical type that results in a loss of precision (i.e. cast double to float, or cast float to int). To fix your CalculateExpToLevel() method, you simply need to explicitly cast the value to int before returning it...

    Code (csharp):
    1.  
    2.     private int CalculateExpToLevel() {
    3.  
    4.         return (int)(_expToLevel * _levelModifier);
    5.  
    6.     }
    7.  
    Also, keep in mind that this is going to round to the nearest integer. For example, if _levelMondifer=0.4 and _expToLevel=1, then the function will return 0. That may or may not be the behavior that you want.
     
    Last edited: Sep 4, 2012
  3. myka

    myka

    Joined:
    Jan 9, 2012
    Posts:
    71
    Thanks, ima see if this works :D
     
    Last edited: Sep 4, 2012