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
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 (this 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 this 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
Is the exception being thrown on this line:
force = ui.returnForce();
Best Answer
Answer by crabeatr · Dec 29, 2015 at 02:22 PM
I solved my problem by editing my code like this, 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);