- Home /
Question by
Breuk · Feb 04, 2014 at 09:16 PM ·
functioncallnullreferenceother script
calling a function from non attached other script
I have 2 scripts
c# ClothingManager.cs:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
public class ClothingManager {
public List<GameObject> addedClothing = new List<GameObject>();
public string [] clothing = {"Jacket", "Pants", "Dress"};
public NiteController niteController;
public ClothingManager(NiteController nc) {
niteController = nc;
}
public void ToggleClothing(string clothingLabel) {
Debug.Log("Toggling " + clothingLabel);
Vector3 location;
niteController.GetJointPosition(NiteWrapper.SkeletonJoint.TORSO_CENTER, out location);
GameObject clothing = FindActiveClothing(clothingLabel);
if (clothing == null) {
clothing = Object.Instantiate(Resources.Load(clothingLabel), location, Quaternion.identity) as GameObject;
clothing.name = clothingLabel;
if (niteController.RegisterRig(clothing)) {
//Add clothing to list of worn clothing
addedClothing.Add(clothing);
} else {
Object.Destroy(clothing);
Debug.LogWarning("Clothing rig not valid: " + clothingLabel);
}
} else {
addedClothing.Remove(clothing);
Object.Destroy(clothing);
}
}
and a javascript:
var clothingManager : ClothingManager;
function Awake()
{
clothingManager = gameObject.GetComponent(ClothingManager); //get the c#script
}
function Pants()
{
Debug.Log("blablablablabalb");
clothingManager.ToggleClothing("Pants");
}
When I call the funtion Pants I get a nullreferenceexception object reference not set to an instance of an object error
How can I solve this?
Comment
Answer by Graham-Dunnett · Feb 04, 2014 at 09:19 PM
Read: http://docs.unity3d.com/Documentation/Manual/ScriptCompileOrderFolders.html to learn how JS and c# interact.
Thank you, but I Already put the c# file in the Standard Assets folder
Did GetComponent
return you a component in your Awake
? You use it assu$$anonymous$$g it's been assigned, which might be a mistake.