(C# - working)(dll - not working) Object reference not set to an instance of an object
i have a project where i have to use a dll made from c# code. The scene has 3 object, 2 objects use script from the dll while the third object has a regular c# script. If you use the C# scripts instead of the dll everything in the scene works fine.
scripts in the dll:
car_2d:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class car_2d : MonoBehaviour {
public float speed=0.0f;
private float speed_max=10.0f;
private float accel=5.0f;
private float deccel;
private float r1_speed=50.0f;
private float r2_speed;
//link to the input deput_control
private deput_control deput_control_link_left_pad;
private deput_control deput_control_link_right_pad;
private string controller_output_left_pad;
private string controller_output_right_pad;
// Use this for initialization
void OnEnable () {
deccel=2*accel;
r2_speed=2*r1_speed;
//link to deput_control
deput_control_link_left_pad=GameObject.Find("deput_left_pad").GetComponent<deput_control>();
deput_control_link_right_pad=GameObject.Find("deput_right_pad").GetComponent<deput_control>();
}
// Update is called once per frame
void Update () {
//get output from deput
controller_output_left_pad=deput_control_link_left_pad.deput_final_output;
controller_output_right_pad=deput_control_link_right_pad.deput_final_output;
//if press turn (left or right) then turn car
if(Input.GetAxis("Horizontal")>0 || controller_output_right_pad=="right"){
transform.Rotate(Vector3.up*r1_speed*Time.deltaTime);
}
if(Input.GetAxis("Horizontal")<0 || controller_output_right_pad=="left"){
transform.Rotate(Vector3.up*-r1_speed* Time.deltaTime);
}
if(Input.GetAxis("Vertical")>0 || deput_control_link_left_pad.deput_active==true){
//if above max speed reach then stay at max
speed+=accel*Time.deltaTime;
if(speed>speed_max){
speed=speed_max;
}
}else{
if(speed>0){
speed-=deccel*Time.deltaTime;
}
if(speed<0){
speed=0.0f;
}
}
//move car
transform.Translate(Vector3.forward*speed*Time.deltaTime);
}
}
car_manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class car_manager : MonoBehaviour {
//link to deputs
private deput_control left_pad_link;
private deput_control right_pad_link;
private deput_control home_pad_link;
//link to shapesboy_manager
private shapesboy_manager shapesboy_manager_link;
private bool system_menu_status_last=false;
//link to objects/ scripts
private car_2d player_car_script;
private GameObject speed_meter;
void configure_pads(){
left_pad_link.deput_setup("2way_v", 0, 0);
right_pad_link.deput_setup("2way_h", 0, 0);
home_pad_link.deput_setup("0way", 0, 0);
}
// Use this for initialization
//void Start () {
void OnEnable () {
//link to shapesboy_manager
shapesboy_manager_link=GameObject.Find("brain").GetComponent<shapesboy_manager>();
//link to deputs needed
left_pad_link=GameObject.Find("deput_left_pad").GetComponent<deput_control>();
right_pad_link=GameObject.Find("deput_right_pad").GetComponent<deput_control>();
home_pad_link=GameObject.Find("deput_home_pad").GetComponent<deput_control>();
//link to objects/ scripts
player_car_script=GameObject.Find("player_car").GetComponent<car_2d>();
speed_meter=GameObject.Find("speed_value");
//configure deput pads
configure_pads();
}
// Update is called once per frame
void Update () {
//get speed from car and post it on speed meter
speed_meter.GetComponent<TextMeshPro>().text=player_car_script.speed.ToString("F1");
}
}
the console gives the following errors:
NullReferenceException: Object reference not set to an instance of an object
car_manager.configure_pads ()
car_manager.OnEnable ()
and
NullReferenceException: Object reference not set to an instance of an object
car_2d.Update ()
i think after changing to a dll from C# that i can not "Find" the game object or GetComponent. For reference when i dont use the dll and use the C# scripts directly it works fine.
Any help or direction is appreciated.
Answer by dee7800 · Jul 03, 2018 at 02:36 AM
i got it working by changing the hosting programs scritps from C# to dll sourced.
what i suppose:
When i made the car scritps dll for car_manager and car_2d i had to include reference dll's for things it needed to know. i made a shapes_dll with things like deput_control in it so i could compile my car scripts. I did not think i would need to update the hosting scene to the shapes_dll scripts but changing that aspect has cleared my issue through trial and error.
Your answer
Follow this Question
Related Questions
Sending output to USBm device (Scent Palette) 0 Answers
Why can't I call a function within the same class? (Or: how does unity handle instancing?) 0 Answers
Is Something Wrong With My Singleton Pattern? 1 Answer
Going NUTS- Tring to return a list from another script 3 Answers
Unity crashing with runtime error, when accessing opencv function in unity. [HELP]!!! 1 Answer