Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Dec 04, 2014 at 05:12 AM by Prasanna for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Prasanna · Feb 19, 2014 at 12:33 PM · movementcharacternullreferenceexceptiondice

Null Reference

Hello there, i having problem with this error.

NullReferenceException: Object reference not set to an instance of an object CharacterMovement.Movement () (at Assets/Scripts/CharacterMovement.cs:16) CharacterMovement.Update () (at Assets/Scripts/CharacterMovement.cs:24)

Here is the code...

using UnityEngine; using System.Collections;

public class CharacterMovement : MonoBehaviour

{

 // Player Movement Variables/....
 public static int movespeed = 1;

 void Start()
 {

 }

 public void Movement()
 {
     if(Switch.GUIValue.text == "Dice Value: 2")
     {
         transform.Translate(Vector3.right * movespeed * Time.deltaTime);
     }
 }

 public void Update()
 {
     Movement();
 }

}

what am trying to do is, i move my character based upon the dice value. Dice value code is given below..

using UnityEngine; using System.Collections;

public class Switch : MonoBehaviour

{

 public static GUIText GUIValue;
 public bool showButton = false;

 void start()
 {
     GUIValue.text = "Dice Value: ";
 }

 public void RunAfterDelay(float delay, System.Action codeToRun)
 {
     StartCoroutine (DoDelay(0.8f, codeToRun));
 }

 IEnumerator DoDelay(float delay, System.Action codeToRun)
 {
     yield return new WaitForSeconds(0.8f);
     codeToRun();
 }

 public void OnGUI()
 {
     if(showButton)
     {
         if(GUI.Button(new Rect(10,50,80,30),"Dice Roll"))
         {
             switch(Random.Range (1, 7))
             {
             default:
             case 1:
                 animation.Play("One");
                 RunAfterDelay(animation["One"].length, ()=> GUIValue.text = "Dice Value: 1 ");
                 transform.position = new Vector3(1.5f, 0.3f, 0.5f);
                 break;
             case 2:
                 animation.Play("Two");
                 RunAfterDelay(animation["Two"].length, ()=> GUIValue.text = "Dice Value: 2 ");
                 break;
             case 3:
                 animation.Play("Three");
                 RunAfterDelay(animation["Three"].length, ()=> GUIValue.text = "Dice Value: 3 ");
                 break;
             case 4:
                 animation.Play("Four");
                 RunAfterDelay(animation["Four"].length, ()=> GUIValue.text = "Dice Value: 4 ");
                 break;
             case 5:
                 animation.Play("Five");
                 RunAfterDelay(animation["Five"].length, ()=> GUIValue.text = "Dice Value: 5 ");
                 break;
             case 6:
                 animation.Play("Six");
                 RunAfterDelay(animation["Six"].length, ()=> GUIValue.text = "Dice Value: 6 ");
                 break;
             }
         }

     }
 }
 
 public void Update()
 {
     showButton = true;
 }

}

If the dice value is shown by 2 means the character needs to move 2 steps in x-Axis..

-Prasanna

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by GameVortex · Feb 19, 2014 at 12:49 PM

Nowhere in your code are you initializing GUIValue, you are only setting GUIValue.text which is not possible before GUIValue has been set. The error does not occur until the Update of CharacterMovement because that is where GUIValue is used first because the Start function in Switch is written wrong and will not be called.

You are also checking for the wrong text in the Movement() function, you set it to: "Dice Value: 2 " and check for "Dice Value: 2". (Notice the space at the end?). I would recommend storing the actual int from the random function and checking against that instead as that is less prone to errors and typos.

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 Prasanna · Feb 19, 2014 at 01:11 PM 0
Share

Thanks for tip...

avatar image
1

Answer by prototype7 · Feb 19, 2014 at 12:50 PM

Maybe you forgot to define your GUIText since its static, so change to public without static and drag and drop your GUIText from inspector

 public GUIText GUIValue;
 public bool showButton = false;
 
 void start()
 {
     GUIValue.text = "Dice Value: ";
 }
 
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 Prasanna · Feb 19, 2014 at 01:11 PM 0
Share

I tried that but its not working...

Follow this Question

Answers Answers and Comments

20 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

Related Questions

CharacterController NullReferenceException dispite attached to object 1 Answer

Cant move character to waypoint on dice roll 1 Answer

Character Controller problem 0 Answers

Character motor movement and jump 1 Answer

Character movement with rotation 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