Help with this code
I'm using this code to present the y value of a moving object into text. There are multiple errors because it does not derive from mono-behavior and other things, what does it mean, and can somebody help fix it?
Code: using System; //using System.ComponentModel; //using System.Reflection; using UnityEngine; using UnityEngine.UI; public static class ExtensionMethods { public static void MoveWithOutput(this Transform transform, Vector3 newPosition, InputField targetInputField) { targetInputField.text = transform.position.y.ToString(); // Global Position Y transform.position = newPosition; } }
And what is your problem? This script is not supposed to be attached on a GameObject (since it does not derive from $$anonymous$$onoBehaviour, because it's a static class defining extensions methods).
You are supposed to create another script and use the extension method.
public class $$anonymous$$yOtherClass : $$anonymous$$onoBehaviour
{
// Drag & drop the inputfield in the inspector
public InputField targetInputField;
private void Update()
{
if( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Space ) )
{
transform.$$anonymous$$oveWithOutput( Random.insideUnitSphere * 5, targetInputField ) ;
}
}
}
Answer by zakkaiokenx10 · Apr 09, 2019 at 07:28 AM
Why is everything commented?
I uncommented everything and uncovered that your class compontentmethods does not derrive from mono behavior.
You can do that by putting " : MonoBehavior" on the class
using System;
using System.ComponentModel;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;
public static class ExtensionMethods : MonoBehaviour {
public static void MoveWithOutput (this Transform transform, Vector3 newPosition, InputField targetInputField) {
targetInputField.text = transform.position.y.ToString (); Global Position Y transform.position = newPosition;
}
}
I don't understand what youre doing, but this will fix that monobehavior problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220612195058im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to assign Text of a Panel child via Script on Panel 0 Answers
How do I get rotation to not jump? 0 Answers
public rigidbody in Javascript 1 Answer
Cannot detect trigger 2 Answers
Reference clock from another script 1 Answer