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 alexandremm · Dec 11, 2016 at 11:28 PM · c#androidscripting problem

Help with C# code + Unity

Hi guys, I'm making a school project and I'm gettin error "CS0649", on the code below:

It says "Field 'GameController.particulas' is never assigned to, and will always have its default value null XadrezUnity.CSharp "

I tried to look everything but I don't went anywhere... It's a chess game, the Aura didn't appears in the game as should appear.

My screen: alt text

Which screen I should get:

alt text

 using UnityEngine;
 using tabuleiro;
 using xadrez;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 class GameController : MonoBehaviour {
 
     public GameObject ReiBranco = null;
     public GameObject ReiPreto = null;
     public GameObject RainhaBranca = null;
     public GameObject RainhaPreta = null;
     public GameObject TorreBranca = null;
     public GameObject TorrePreta = null;
     public GameObject CavaloBranco = null;
     public GameObject CavaloPreto = null;
     public GameObject BispoBranco = null;
     public GameObject BispoPreto = null;
     public GameObject PeaoBranco = null;
     public GameObject PeaoPreto = null;
 
 
     public Text txtMsg = null;
     public Text txtXeque = null;
 
     public GameObject pecaEscolhida { get; private set; }
 
     public Estado estado { get; private set; }
 
     PartidaDeXadrez partida;
     PosicaoXadrez origem, destino;
     Color corOriginal;
     Vector3 posDescarteBrancas, posDescartePretas;
 
     public GameObject particulas;
 
     List<GameObject> listaParticulas;
     
     void Start () {
         estado = Estado.AguardandoJogada;
         pecaEscolhida = null;
         corOriginal = txtMsg.color;
         listaParticulas = new List<GameObject>();
 
         posDescarteBrancas = new Vector3(-2.5f, 0f, -1);
         posDescartePretas = new Vector3(2.5f, 0f, 1);
 
         partida = new PartidaDeXadrez();
 
         txtXeque.text = "";
         InformarAguardando();
 
 
         Util.instanciarTorre('a', 1, Cor.Branca, partida, TorreBranca);
         Util.instanciarCavalo('b', 1, Cor.Branca, partida, CavaloBranco);
         Util.instanciarBispo('c', 1, Cor.Branca, partida, BispoBranco);
         Util.instanciarRainha('d', 1, Cor.Branca, partida, RainhaBranca);
         Util.instanciarRei('e', 1, Cor.Branca, partida, ReiBranco);
         Util.instanciarBispo('f', 1, Cor.Branca, partida, BispoBranco);
         Util.instanciarCavalo('g', 1, Cor.Branca, partida, CavaloBranco);
         Util.instanciarTorre('h', 1, Cor.Branca, partida, TorreBranca);
         Util.instanciarPeao('a', 2, Cor.Branca, partida, PeaoBranco);
         Util.instanciarPeao('b', 2, Cor.Branca, partida, PeaoBranco);
         Util.instanciarPeao('c', 2, Cor.Branca, partida, PeaoBranco);
         Util.instanciarPeao('d', 2, Cor.Branca, partida, PeaoBranco);
         Util.instanciarPeao('e', 2, Cor.Branca, partida, PeaoBranco);
         Util.instanciarPeao('f', 2, Cor.Branca, partida, PeaoBranco);
         Util.instanciarPeao('g', 2, Cor.Branca, partida, PeaoBranco);
         Util.instanciarPeao('h', 2, Cor.Branca, partida, PeaoBranco);
 
 
         Util.instanciarTorre('a', 8, Cor.Preta, partida, TorrePreta);
         Util.instanciarCavalo('b', 8, Cor.Preta, partida, CavaloPreto);
         Util.instanciarBispo('c', 8, Cor.Preta, partida, BispoPreto);
         Util.instanciarRainha('d', 8, Cor.Preta, partida, RainhaPreta);
         Util.instanciarRei('e', 8, Cor.Preta, partida, ReiPreto);
         Util.instanciarBispo('f', 8, Cor.Preta, partida, BispoPreto);
         Util.instanciarCavalo('g', 8, Cor.Preta, partida, CavaloPreto);
         Util.instanciarTorre('h', 8, Cor.Preta, partida, TorrePreta);
         Util.instanciarPeao('a', 7, Cor.Preta, partida, PeaoPreto);
         Util.instanciarPeao('b', 7, Cor.Preta, partida, PeaoPreto);
         Util.instanciarPeao('c', 7, Cor.Preta, partida, PeaoPreto);
         Util.instanciarPeao('d', 7, Cor.Preta, partida, PeaoPreto);
         Util.instanciarPeao('e', 7, Cor.Preta, partida, PeaoPreto);
         Util.instanciarPeao('f', 7, Cor.Preta, partida, PeaoPreto);
         Util.instanciarPeao('g', 7, Cor.Preta, partida, PeaoPreto);
         Util.instanciarPeao('h', 7, Cor.Preta, partida, PeaoPreto);
 
     }
 
     public void processarMouseDown(GameObject obj, GameObject casa)
     {
         if (estado == Estado.AguardandoJogada)
         {
             if (casa != null)
             {
                 try
                 {
                     char coluna = casa.name[0];
                     int linha = casa.name[1] - '0';
                     origem = new PosicaoXadrez(coluna, linha);
                     partida.validarPosicaoDeOrigem(origem.toPosicao());
                     pecaEscolhida = obj;
                     estado = Estado.Arrastando;
                     txtMsg.text = "Selecione a casa de destino";
                     instanciarParticulas();
                 }
                 catch (TabuleiroException e)
                 {
                     InformarAviso(e.Message);
                 }
             }
         }
         else if (estado == Estado.Arrastando)
         {
             GameObject casaDestino = null;
             if (obj.layer == LayerMask.NameToLayer("Casas"))
             {
                 casaDestino = obj;
             }
             else
             {
                 casaDestino = casa;
             }
 
 
             if (casaDestino != null && pecaEscolhida != null)
             {
                 try
                 {
                     char coluna = casaDestino.name[0];
                     int linha = casaDestino.name[1] - '0';
                     destino = new PosicaoXadrez(coluna, linha);
 
                     partida.validarPosicaoDeDestino(origem.toPosicao(), destino.toPosicao());
                     Peca pecaCapturada = partida.realizaJogada(origem.toPosicao(), destino.toPosicao());
 
                     if (pecaCapturada != null)
                     {
                         removerObjetoCapturado(pecaCapturada);
                     }
 
                     pecaEscolhida.transform.position = Util.posicaoNaCena(coluna, linha);
 
                     tratarJogadasEspeciais();
 
                     pecaEscolhida = null;
 
                     if (partida.terminada)
                     {
                         estado = Estado.GameOver;
                         txtMsg.text = "Vencedor: " + partida.jogadorAtual;
                         txtXeque.text = "XEQUEMATE";
                     }
                     else
                     {
                         estado = Estado.AguardandoJogada;
                         InformarAguardando();
                         Invoke("girarCamera", 0.5f);
                         txtXeque.text = (partida.xeque) ? "XEQUE" : "";
                     }
                 }
                 catch (TabuleiroException e)
                 {
                     pecaEscolhida.transform.position = Util.posicaoNaCena(origem.coluna, origem.linha);
                     estado = Estado.AguardandoJogada;
                     InformarAviso(e.Message);
                 }
                 finally
                 {
                     destruirParticulas();
                 }
 
             }
         }
     }
 
     void InformarAviso(string msg)
     {
         txtMsg.color = Color.red;
         txtMsg.text = msg;
         Invoke("InformarAguardando", 1f);
     }
 
     void InformarAguardando()
     {
         txtMsg.color = corOriginal;
         txtMsg.text = "Aguardando jogada: " + partida.jogadorAtual;
     }
 
     void removerObjetoCapturado(Peca peca)
     {
         GameObject obj = peca.obj;
         if(peca.cor == Cor.Branca)
         {
             obj.transform.position = posDescarteBrancas;
             posDescarteBrancas.z = posDescarteBrancas.z + 0.4f; 
         }
         else
         {
             obj.transform.position = posDescartePretas;
             posDescartePretas.z = posDescartePretas.z - 0.4f;
         }
     }
 
     void tratarJogadasEspeciais()
     {
         Posicao pos = destino.toPosicao();
         Peca pecaMovida = partida.tab.peca(pos);
 
         if(pecaMovida is Rei && destino.coluna == origem.coluna + 2)
         {
             GameObject torre = partida.tab.peca(pos.linha, pos.coluna - 1).obj;
             torre.transform.position = Util.posicaoNaCena('f', origem.linha);
         }
 
         if (pecaMovida is Rei && destino.coluna == origem.coluna - 2)
         {
             GameObject torre = partida.tab.peca(pos.linha, pos.coluna + 1).obj;
             torre.transform.position = Util.posicaoNaCena('d', origem.linha);
         }
 
         if(partida.promovida != null)
         {
             removerObjetoCapturado(partida.promovida);
             Vector3 posPromovida = Util.posicaoNaCena(destino.coluna, destino.linha);
             GameObject prefab = (pecaMovida.cor == Cor.Branca) ? RainhaBranca : RainhaPreta;
             GameObject rainha = Instantiate(prefab, posPromovida, Quaternion.identity) as GameObject;
             pecaMovida.obj = rainha;
         }
     }
 
     void girarCamera()
     {
         if(partida.jogadorAtual == Cor.Branca)
         {
             Camera.main.GetComponent<CameraRotacao>().irParaBranca();
         }
         else
         {
             Camera.main.GetComponent<CameraRotacao>().irParaPreta();
         }
     }
     void instanciarParticulas()
     {
         listaParticulas.Clear();
         float y = GameObject.Find("PlanoDasPecas").transform.position.y;
 
         bool[,] mat = partida.tab.peca(origem.toPosicao()).movimentosPossiveis();
         for (int i = 0; i < partida.tab.linhas; i++)
         {
             for (int j = 0; j < partida.tab.colunas; j++)
             {
                 if (mat[i, j])
                 {
                     char coluna = (char)('a' + j);
                     int linha = 8 - i;
                     Vector3 posCasa = GameObject.Find("" + coluna + linha).transform.position;
                     Vector3 pos = new Vector3(posCasa.x, y, posCasa.z);
                     GameObject obj = Instantiate(particulas, pos, Quaternion.identity) as GameObject;
                     listaParticulas.Add(obj);
                 }
             }
         }
     }
     void destruirParticulas()
     {
         foreach (GameObject obj in listaParticulas){
             Destroy(obj);
         }
         listaParticulas.Clear();
     }
 }
 

right.png (177.1 kB)
wrong.png (220.9 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by michelle12188 · Dec 11, 2016 at 11:38 PM

the GameObject particulas is public and you never seem to assign a value to it. So I imagine that you haven't assigned a value in the inspector and then when it tries to do instanciarParticulas() it is trying to instantiate a null object.

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

301 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Help! Values from previous Serialized List<> shows up again after a while using Coroutine 0 Answers

Not able to retrieve information from script in unity. 0 Answers

How can I Build And Run my Unity Game, if I am facing some Player Build Errors? I have added the Errors in details below. 0 Answers

Can this script be better optimized? 1 Answer

Code not working as it should 1 Answer


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