- Home /
Get the colliders transform (?)
I'm not really sure how to put this but i get a NullReferenceException error in the AITrigger script, probably because the collider doesn't have a transform attached to it but i need to access the transform on the same gameobject as the collider is attached to. The question is how? Here is the scripts (Only relevant parts):
AITrigger.cs
void OnTriggerEnter(Collider other) {
if (IsEnemy == "Yes") {
if(other.gameObject.tag == "Friendly" || other.gameObject.tag == "Player") {
PlayerPrefs.SetString(gameObject.name, "Yes");
AIPath AIPath = GetComponent<AIPath>();
sendthis = other.transform;
AIPath.SetTarget(sendthis);
}
}
if (IsEnemy == "No") {
if(other.gameObject.tag == "Enemy") {
PlayerPrefs.SetString(gameObject.name, "Yes");
AIPath AIPath = GetComponent<AIPath>();
sendthis = other.transform;
AIPath.SetTarget(sendthis);
}
}
}
AIPath.cs
public void SetTarget (Transform x) {
target = x;
}
This might come off as a silly question, but have you tried using other.transform? A Collider will always have a transform attached to it.
Nope same thing, NullReferenceException: Object reference not set to an instance of an object AITrigger.OnTriggerEnter (UnityEngine.Collider other) (at Assets/FPS $$anonymous$$it Version 2.0/_CustomAssets/Scripts/Network C#/AITrigger.cs:24)
$$anonymous$$iraSensei: If is not too much to ask for, could you please show me an example? There is non available at the docs.
Jamora: Line 24 is line 7. Yes i have read the second link and as i said in my question its probably because the collider doesn't have a transform attached to it.
Answer by Nexonity · Jan 21, 2014 at 04:46 PM
Somehow after refreshing the script it did work... Anyway thanks for all the suggestions!
What do you mean by refreshing the script ? A script can't be "refreshed", it is modified, or not, and compiled.
Restarted Unity, rightclicked the script and then refresh.