Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by BeardyRamen · Dec 10, 2015 at 07:32 PM · noobdefaultconstructorbasicsunexpected

[SOLVED] Constructors with unexpected default values

Hi all. I am relatively new to coding and game development, and i'm working on a little project that i'm letting grow small step by small step. It is a Tile-TurnBased chesslike game.

I am having some sort of "silly" problem with constructors and I really can't get to decipher C# MSDN guides.

In particular i have two problems: 1. i have my Tile class which is:

 public class Tile 
 {
     public int dimensioneTile;
 
     public int xRel;
     public int yRel;
 
     public bool tileOccupata;
 
     public Tile(int _xRel, int _yRel, bool _occupata)
     {
         xRel = _xRel;
         yRel = _yRel;
         tileOccupata = _occupata;
     }

(tileOccupata is a "isOccupied" sort of bool) it works actually, but since all of my tiles "are born" empty I would like to have somthing like:

 public class Tile 
 {
     public int dimensioneTile;
 
     public int xRel;
     public int yRel;
 
     public bool tileOccupata;
 
     public Tile(int _xRel, int _yRel)
     {
         xRel = _xRel;
         yRel = _yRel;
         tileOccupata = false;
     }
 }

Where the variable already stores the information of being "empty", but if i code it like so it stores it as true, ignoring my line of code saying "tileOccupata = false"... this is not the behavior i was expecting.


My second problem is similar but slightly different

 public class Eroe  
 {
     public string nome;
     public int Strength;
     //i want health to be Strength * 2
     public int Health;
     
 //constructor
 public Eroe(string _nome, int STR)
     {
         nome = _nome;
 
         Strength = STR;
         Health = Strength * 2;
            //i  also tried Health = STR * 2
     }
 }

in both cases Health turns out to be 0

So my actual question is: do i need to have a parameter for each variable i have in my class for it to be actually initialized at a different value than 0 || true? How can I do it?

Thank you for your help

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave-Carlile · Dec 10, 2015 at 07:54 PM 0
Share

How are you calling these? In the first case tileOccupata should definitely be false. It should be false even if you don't set it since that is the default value. I would guess you're setting it elsewhere.

The second case is similar - if you're passing in a non-zero value for STR then Health should be set to double that. It's also public so could be set elsewhere.

avatar image BeardyRamen Dave-Carlile · Dec 10, 2015 at 07:57 PM 0
Share

I am pretty sure that i don't set them elsewhere. I actually never call it except in "Debug.Log" when i call the constructor like Eroe pawn = new Eroe(pawn, 5); Debug.Log(pawn.Health); it prints 0.

same with Tile.tileOccupata; it prints true. No idea why

btw i will duoblecheck

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BeardyRamen · Dec 10, 2015 at 08:05 PM

I found the error. As I thought it was a noob one. I had stored some "Eroe" before i added the variable "Health". I was running the game using the old instances that didn't have Health set

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need car track construction 0 Answers

Changing material dose not change the appearance of the game object 0 Answers

Combo mechanism for button clicker? 1 Answer

Problem with nested Start Coroutine in loop 1 Answer

A good Tutorial for Character Selection with Networking?? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges