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

Class constructs null

Discussion in 'Scripting' started by Kainanteh, Jul 29, 2016.

  1. Kainanteh

    Kainanteh

    Joined:
    Sep 3, 2014
    Posts:
    14
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class TrofologiaManager : MonoBehaviour
    6. {
    7.  
    8.  
    9.     public static TrofologiaManager instance = null;
    10.  
    11.     public Indice_tipos frutas_acidas;
    12.     public Alimento citricos;
    13.  
    14.  
    15.     void Awake ()
    16.     {
    17.    
    18.    
    19.  
    20.         if (instance == null)
    21.         {
    22.                
    23.                 instance = this;
    24.        
    25.         }
    26.     }
    27.    
    28.  
    29.     void Start ()
    30.     {
    31.  
    32.  
    33.  
    34.         frutas_acidas =  new Indice_tipos (1,"Frutas ácidas",true,frutas_acidas_comb);
    35.         Debug.Log (frutas_acidas.nombre_tipo);
    36.  
    37.  
    38.         citricos = new Alimento (1,"Cítricos",TrofologiaManager.instance.frutas_acidas);
    39.         Debug.Log(citricos.nombre_alimento);
    40.  
    41.     }
    42.  
    43.  
    44. }
    45.  

    Code (CSharp):
    1. public class Indice_tipos  {
    2.  
    3.     public int indice;
    4.     public string nombre_tipo;
    5.     public bool se_combina_consigo;
    6.     public List<Indice_tipos> combinaciones;
    7.  
    8.     public Indice_tipos(int i, string n, bool scc, List<Indice_tipos> c)
    9.     {
    10.         i = indice;
    11.         n = nombre_tipo;
    12.         scc = se_combina_consigo;
    13.         c = combinaciones;
    14.     }
    15.  
    16. }

    Code (CSharp):
    1. public class Alimento  {
    2.  
    3.     public int indice;
    4.     public string nombre_alimento;
    5.     public Indice_tipos tipo_alimento;
    6.  
    7.  
    8.     public Alimento(int i,string n, Indice_tipos t)
    9.     {
    10.         i = indice;
    11.         n = nombre_alimento;
    12.         t = tipo_alimento;
    13.     }
    14.  
    15. }
    16.  

    The result of the debugs is null, any idea why?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    Yup, your constructors are all wrong. You're assigning the fields of your objects to the input arguments instead of the other way around.

    Flip the operands of all of the assignments in Alimento and Indice_tipos, ie:

    Code (csharp):
    1. //Wrong:
    2. i = indice;
    3.  
    4. //Correct:
    5. indice = i;
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    By the way, do yourself a favour and write code in English. You seriously reduce the number of people who can help you with your code if you don't. It's how all professional software is written, so you'll also probably have to do it if you get a job or join an open source project, so it's a good habit.

    Since all of the built-in stuff is English too, you end up with a weird mix:

    Code (csharp):
    1. public class Alimento
    One sentence, two languages!

    Trust me, it's not hard to get used to, and the benefits are huge. I'm not a native English speaker myself, so don't take this as cultural imperialism, it's just a better way to do things.
     
  4. Kainanteh

    Kainanteh

    Joined:
    Sep 3, 2014
    Posts:
    14
    jaja I knew it would be some foolish thing, thanks for that.

    To the thing of the languages, im spanish and there not such a thing for "public class" into my language, i know what you say, and i work mostly my name variables in english, but this are a special case.