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

Unity Warning preventing mechanic

Discussion in 'Scripting' started by Pyronide, Apr 15, 2015.

  1. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    I have been trying to look into this bug for a while and can’t seem to find a solution, so I am posting here to get some help. Now, I will say that I am not interested in a direct fix per se, but as I am new to coding still and Unity in general I am more interested in an answer that also explains why this happened so I can try to avoid it in the future, so here goes…

    I am building an incremental game that, when clicked, will hire a Punk (granted they have enough requirements) when they hire the Punk it is supposed to increase the total amount of income by say 0.02 (What I have right now). The problem I noticed is that when he is hired, the “PunkH.increase” doesn’t get added, so I am unable to use it for the proper math… It isn’t an error, but a warning, either way it needs to be dealt with so I can create this game… warning says “BCW0028: WARNING: Implicit downcast from “Object” to “int”

    Here is the relevant code:

    Code (csharp):
    1. var PunkH = new Hire (10, 0.02);
    2. var Punk : int = 0;
    3.  
    4.  
    5. class Hire {
    6.   var cost : int;
    7.   var increase : int;
    8.  
    9.   function Hire (cost, increase) {
    10.   this.cost = cost;
    11.   this.increase = increase;
    12.  
    13.   }
    14.  
    15. }
    If there is anything else needed please let me know and I will try to provide as much information as I can. Thanks for the help!
     
  2. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    It's simple, you're not giving a type to your cost, and for antoher problem try to change your temp variable. Well, like this :

    Code (JavaScript):
    1.  
    2. var PunkH = new Hire (10, 0.02);
    3. var Punk : int = 0;
    4.  
    5. class Hire {
    6.   var cost : int;
    7.   var increase : int;
    8.  
    9.   function Hire (_cost : int, _increase : int) {
    10.       this.cost = _cost;
    11.       this.increase = _increase;[/INDENT]
    12.   }
    13. }
    14.  
     
  3. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    OK, I think I understand this... Thanks very much.

    EDIT: So this took the warning away and does seem to work for Cost, but it isn't working for increase, I tried switching it to a float thinking that it was because ints don't take decimals, but that didn't help either.
     
    Last edited: Apr 15, 2015