- Home /
NullReferenceException: Object reference not set to an instance of an object (when doing js to add value to c#)
here is the js:
private var mn;
private var CsharpScript;
var oscChannel:int;
function start() {
mn = GetComponent("MegaNoise");
}
function Update () {
var n = OSCReceiver.messages[oscChannel];
transform.localScale = Vector3(n+0.3,n+0.3,n+0.3);
mn.Freq = n+1;
//Debug.Log(mn.Freq);
}
i just need to add the value from osc input to the cSharp logic i have here :
using UnityEngine;
[AddComponentMenu("Modifiers/Noise")]
public class MegaNoise : MegaModifier
{
public float Scale = 1.0f;
public bool Fractal = false;
public float Freq = 0.25f;
public float Iterations = 6.0f;
public bool Animate = false;
public float Phase = 0.0f;
public float Rough = 0.0f;
public Vector3 Strength = new Vector3(0.0f, 0.0f, 0.0f);
MegaPerlin iperlin = MegaPerlin.Instance;
float time = 0.0f;
float scale;
float rt;
Vector3 sp = Vector3.zero;
Vector3 d = Vector3.zero;
public override string ModName() { return "Noise"; }
public override string GetHelpURL() { return "?page_id=262"; }
public override void Modify(MegaModifiers mc)
{
for ( int i = 0; i < verts.Length; i++ )
{
Vector3 p = tm.MultiplyPoint3x4(verts[i]);
sp.x = p.x * scale + 0.5f;
sp.y = p.y * scale + 0.5f;
sp.z = p.z * scale + 0.5f;
if ( Fractal )
{
d.x = iperlin.fBm1(sp.y, sp.z, time, rt, 2.0f, Iterations);
d.y = iperlin.fBm1(sp.x, sp.z, time, rt, 2.0f, Iterations);
d.z = iperlin.fBm1(sp.x, sp.y, time, rt, 2.0f, Iterations);
}
else
{
d.x = iperlin.Noise(sp.y, sp.z, time);
d.y = iperlin.Noise(sp.x, sp.z, time);
d.z = iperlin.Noise(sp.x, sp.y, time);
}
p.x += d.x * Strength.x;
p.y += d.y * Strength.y;
p.z += d.z * Strength.z;
sverts[i] = invtm.MultiplyPoint3x4(p);
}
}
public override void ModStart(MegaModifiers mc)
{
}
public override Vector3 Map(int i, Vector3 p)
{
p = tm.MultiplyPoint3x4(p);
float spx = p.x * scale + 0.5f;
float spy = p.y * scale + 0.5f;
float spz = p.z * scale + 0.5f;
float dx,dy,dz;
if ( Fractal )
{
dx = iperlin.fBm1(spy, spz, time, rt, 2.0f, Iterations);
dy = iperlin.fBm1(spx, spz, time, rt, 2.0f, Iterations);
dz = iperlin.fBm1(spx, spy, time, rt, 2.0f, Iterations);
}
else
{
dx = iperlin.Noise(spy, spz, time);
dy = iperlin.Noise(spx, spz, time);
dz = iperlin.Noise(spx, spy, time);
}
p.x += dx * Strength.x;
p.y += dy * Strength.y;
p.z += dz * Strength.z;
return invtm.MultiplyPoint3x4(p);
}
//public override bool ModLateUpdate(Modifiers mc)
public override bool ModLateUpdate(MegaModContext mc)
{
if ( Animate )
Phase += Time.deltaTime * Freq;
time = Phase;
return Prepare(mc);
}
public override bool Prepare(MegaModContext mc)
{
// Need this in a GetDeformer type method, then drawgizmo can be common
if ( Scale == 0.0f )
scale = 0.000001f;
else
scale = 1.0f / Scale;
rt = 1.0f - Rough;
return true;
}
}
and then i got this wired error:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value) ScaleObject.Update () (at Assets/Scripts/ScaleObject.js:13)
the c# file name is MegaNoise.cs and it works with the string "MegaNoise" over another project , but in here it doesn't wondering why!? any one please help!
Answer by Graham-Dunnett · Jul 08, 2015 at 09:55 AM
When you do GetComponent() it's 100% important that you check the return value. If you do:
function start() {
mn = GetComponent("MegaNoise");
Debug.Log(mn);
}
then you'll learn something important. (Which to save you the time, is that this function won't be executed. Because its function name is incorrect. Lower-case 's' is your error.)