• 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 /
This question was closed Dec 29, 2015 at 02:22 PM by crabeatr for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by crabeatr · Dec 28, 2015 at 02:40 PM · scripting problemfunctionreturnfunction callreturn value

Script not returning value to other script

Hi, basically my UI script gets value of force from user and converts it to float (t$$anonymous$$s much working correctly)

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 public class miUI : MonoBehaviour {
 
     public Text forceText;
     public Move move;
     public static float force=0;
     public FirstPersonController fps;
 
 
     // Use t$$anonymous$$s for initialization
     void Start () {
         forceText.text = "Current Force : 0.0 N ";
 
     }
     
     public void CharacterField(string InputFieldString)
     {
         forceText.text = InputFieldString ;
         force = float.Parse (InputFieldString);
         Debug.Log (force);
     }
     public float returnForce()
     {   Debug.Log (force);
         return force;
     }
 }

I've got a script on my projectile prefab where it tries to get return value from returnForce function from UI script.But, instantiating my prefab instead gives the error that Object reference not set to an instance of object. Here's the script on my prefab.

 using UnityEngine;
 using System.Collections;
 
 public class Move : MonoBehaviour {
 
     Rigidbody rb;
     public static float force;
     public miUI ui;
 
     void Start () { 
         force = ui.returnForce();
         rb= GetComponent<Rigidbody>();
         rb.AddForce (transform.forward * force);
         Debug.Log (force);
     }
 
     public void forche( float inputForce)
     {
         force = inputForce;
     }
 }
 

All help appreciated !!

Edit: Exception is being thrown on line force = ui.returnForce();

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 Landern · Dec 28, 2015 at 02:57 PM 0
Share

Is the exception being thrown on this line:

 force = ui.returnForce();
avatar image crabeatr Landern · Dec 28, 2015 at 05:45 PM 0
Share

yes it is.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by crabeatr · Dec 29, 2015 at 02:22 PM

I solved my problem by editing my code like t$$anonymous$$s, for anyone else facing the same problem :

 public class Move : MonoBehaviour {
     GameObject mein1;
     Text txt1;
     Rigidbody rb;
     public static float force;
 
     void Start () { 
         mein1 = GameObject.Find ("forceText");
         txt1 = mein1.GetComponent<Text> ();
         force = float.Parse (txt1.text);
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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Using function return in if statement 2 Answers

How to return a function 0 Answers

Not all code paths return a value error. Fixing the error breaks my function. Ideas? 1 Answer

Variable from Void not working 1 Answer

Using return correctly? 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