• 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 /
avatar image
0
Question by ncgty · May 06, 2011 at 03:59 AM · nullreferenceexception

New to Unity3d - NullReferenceException

Hello everyone. Im new to Unity3d programming and I wanted to know whats going wrong on my code. Ive got this error but I dont know why actually:

NullReferenceException: Object reference not set to an instance of an object.

ChangeColor.cs

using UnityEngine; using System.Collections;

public class ChangeColor : MonoBehaviour {

 public Color corAcesa, corApagada;
 public int numeroCubo;
 public GameManagerAula2 gameManager;

 public void OnMouseDown() {
     if(gameManager.getTurno()==1) {
         gameManager.setNumeroDoCubo(getNumeroCubo());
         StartCoroutine(acenderCor());
     }
 }

 public IEnumerator acenderCor() {

     renderer.material.color=getCorAcesa();
     yield return new WaitForSeconds(0.5f);
     renderer.material.color=getCorApagada();
 }

 public int getNumeroCubo() {
     return this.numeroCubo;
 }

 public Color getCorAcesa() {
     return this.corAcesa;
 }

 public Color getCorApagada() {
     return this.corApagada;
 }

 public void iniciaCubo() {
     renderer.material.color=this.getCorApagada();
 }

 public void resetaCubo() {
     renderer.material.color=Color.black;
 }

}

GameManagerAula2.cs

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class GameManagerAula2 : MonoBehaviour {

 public List<ChangeColor> listaCubo; //lista dos cubos
 public List<int> ordem;             //ordem dos cubos
 public int cubo;                    //cubo selecionado
 public int numeroTurno=0;           //numero do turno
 public int turno=0;                 //0=computer, 1=player

 void Start() {
     iniciaCor();
     StartCoroutine(turnoComputador());
 }

 public IEnumerator turnoComputador() {

     cubo=Random.Range(0,4);
     ordem.Add(cubo);

     //adicionar tempo a primeira jogada
     if (numeroTurno==0) {
         yield return new WaitForSeconds(1.5f);
     }

     foreach (int i in this.ordem) {
         StartCoroutine(listaCubo[i].acenderCor());
         yield return new WaitForSeconds(1.5f);
     }

     mudaTurnoJogador();
     StartCoroutine(turnoJogador());
     yield return 0;
 }

 public IEnumerator turnoJogador() {

     while(cubo==-1) {
         yield return 0;
     }

     //acerto
     if(ordem[numeroTurno]==cubo) {
         cubo=-1;
         incrementaNumeroTurno();
         if(numeroTurno==ordem.Count) {
             resetaNumeroTurno();
             mudaTurnoComputador();
             StartCoroutine(turnoComputador());
         }
         else {
             mudaTurnoJogador();
             StartCoroutine(turnoJogador());
         }           
     }
     //erro
     else {
         resetaJogo();
         yield return new WaitForSeconds(2);
         iniciaCor();
         StartCoroutine(turnoComputador());
     }

     yield return 0;
 }

 public int getTurno() {
     return this.turno;
 }

 public void mudaTurnoComputador() {
     this.turno = 0;
 }

 public void mudaTurnoJogador() {
     this.turno = 1;
 }

 public void resetaNumeroTurno() {
     this.numeroTurno = 0;
 }

 public void incrementaNumeroTurno() {
     this.numeroTurno++;
 }

 public void setNumeroDoCubo(int numeroDoCubo) {
     this.cubo = numeroDoCubo;
 }

 public void iniciaCor() {
     for(int i=0;i<=3;i++) {
         listaCubo[i].iniciaCubo();
     }
 }

 public void resetaCor() {
     for(int i=0;i<=3;i++) {
         listaCubo[i].resetaCubo();
     }
 }

 public void resetaOrdem() {
     this.ordem.Clear();
 }

 public void resetaJogo() {
     this.mudaTurnoComputador();
     this.resetaOrdem();
     this.resetaCor();
 }

}

Ive got the get/set on GameManagerAula2 class. I followed an example where that would works but as I coded I just realized that something is wrong and I really dont know why. This is no serious business work or something like that, its just some exercise from some Unity3d classes Im having (but since there are some days left until next class I`m asking it here).

This is a little game where the pc chooses a random cube (4 cubes) and wait for the player to click on it. Then the pc plays again and choose the previous cube +1, (...) rinse and repeat.

I know the game isnt ready yet but this error is freaking me out because I cant test the click.

Thanks in advance.

Comment
Add comment · Show 1
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 Alec-Slayden · May 06, 2011 at 08:05 AM 0
Share

the error console should tell you the line that threw the null reference error in the details. That will help you greatly in pinpointing the problem(s).

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Henrique Vilela · May 06, 2011 at 04:04 AM

Looks like you forgot to set the variables corAcesa, corApagada and/or gameManager though the inspector. Am I right?

Comment
Add comment · Show 1 · 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
avatar image ncgty · May 06, 2011 at 01:26 PM 0
Share

actually I did the corAcesa, corApagada and also numero, but yeah you lighten me up.. I didn`t set the game$$anonymous$$anager thru the inspector. ><

so silly but it was soo late I coudldnt think of that.. thanks.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Missing Reference after null Check 4 Answers

Name-fetching script working but with error. 1 Answer

Null reference exception on Screenpointtoray (Multiplayer) 1 Answer

Need Help With a NullReference Exception 2 Answers

NullReferenceException 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