Hi everyone,
I know that this question is redundant but I’ve checked almost every post close to my problem without finding a solution so here it is :
I have a GameObject named “Ulysse” with a script named “UlysseMover”. Into this script, I call a public method in a script named “Pilier1” located on a other GameObject named “Pilier1”.
I made the links betweeen scripts so there is no problem on code but when I try to access the method I’ve got an error saying “Object reference not set to an instance of an object”…
==========================================================
UlysseMover.cs
using UnityEngine;
using System.Collections;
public class UlysseMover : MonoBehaviour {
//Some code
//In "public void Update()"
Pilier1 scrPilier1 = GetComponent<Pilier1>();
//Code again
//In "public void Update() too"
scrPilier1.ActionEffect();
//Still some code
}
============================================================
Pilier1.cs
using UnityEngine;
using System.Collections;
public class Pilier1 : MonoBehaviour {
public void ActionEffect () {
rigidbody.velocity = new Vector3(0,-10,0);
}
}
============================================================
Is anyone has the solution ? Thanks !
Hello everyone !
@Robertbu : I had already checked that it wasn’t an error of null object etc… but thanks !
@EliteMossy : Thanks ! My problem was solved by searching this way even if I have to add some lines^^
So here is what is working for me :
=========================================================
UlysseMover.cs
public class UlysseMover : MonoBehaviour {
[HideInInspector] private Pilier1 pilierscript;
public void Update() {
GameObject objetcourant = scrActions.closestObjectActive;
pilierscript = objetcourant.GetComponent<Pilier1>();
pilierscript.ActionEffect();
}
}
==========================================================
Pilier1.cs
public class Pilier1 : MonoBehaviour {
public void ActionEffect () {
gameObject.rigidbody.velocity = new Vector3(0,0,-1);
}
}
==========================================================
Again thanks to the people who answered so quickly ! PROBLEM SOLVED !!!
Have a nice day !
Thanks to both of you ! My problem is solved ! Here is the code I add :
UlysseMover.cs
public class UlysseMover : MonoBehaviour {
private Pilier1 pilierscript;
public void Update() {
GameObject objectcourant = scrActions.closestObjectActive;
pilierscript = objectcourant.GetComponent<Pilier1>();
pilierscript.ActionEffect();
}
}
Thanks again for your quick answers ! ==> PROBLEM SOLVED !
GetComponent will only get the component on the CURRENT GameObject. If it is on another gameobject, you need to store a reference to the other GameObject or do a search for the GameObject.
Now you can either store it globally, like
using UnityEngine;
using System.Collections;
public Pilier1 srcPilier1;
public class UlysseMover : MonoBehaviour {
//Some code
//In "public void Update()"
//Code again
//In "public void Update() too"
scrPilier1.ActionEffect();
//Still some code
}
and simply drag the GameObject with the Pilier1 script in the inspector. Or you can simply use Start and do a find. More info is explained here:
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Good luck 