- Home /
GetComponent not working
I have a gameobject called CPJ that has a script (Row Controller) attached to it, which has a method called TransferRow.
In another gameobjects script i want to call TransferRow and i call it as follows
public GameObject CRJ;
public RowController CRJRC;
public void Method1()
{
CRJ = GameObject.Find("CRJ");
CRJRC = CRJ.GetComponent<RowController>();
CRJRC.TransferRow(int.Parse(df.txtDay.text), df.txtDetails.text, df.txtBank.text, df.txtSundryDetails.text, "CPJRow", df.CoinType);
}
This gives me a nullreferenceException on the line CRJRC.TransferRow(...);
CRJ is called correctly as a simple debug.log confirms.
In the inspector on the gameobject doing the calling, the public variable CRJ is filled correctly, and CRJRC is also present as seen below.
Im not sure as to why the nullReferenceException occurs and i have tried calling it in other ways, but with no success.
I am not adding the RowController script reference directly to my "Send to other Journal" script via the inspector, as the the object that the script is attached to is important and used in the script.
Please Advise on how i can call this method, or if you see any errors that i have missed. if you need any extra information i will gladly supply it.
TransferRow code
public void TransferRow(int Day, string Details, string Amount, string SundryDetails, string Journal, string CoinType)
{GameObject row = Instantiate(OtherBookRow, OtherBookContents).gameObject;}
Can you share CRJRC.TransferRow code. Is it Public Void ?
yes CRJRC is public void
looks like this
public void TransferRow(int Day, string Details, string Amount, string SundryDetails, string Journal, string CoinType)
{GameObject row = Instantiate(OtherBookRow, OtherBookContents).gameObject; }
Can you change any variable in RowController from SendToOtherJournal ? Try to change any float or int. If it works, then you are not sending all/correct variables to the Void ((int Day, string Details, string Amount, string SundryDetails, string Journal, string CoinType))
What are the ..... marks in CRJRC.TransferRow(.....); They will cause a problem.
... just replacing the bunch of variables in the call
I updated the question and added the complete call
Answer by chrisjacobsz97 · Jun 10, 2018 at 06:33 PM
Problem Solved
As @bakir-omarov mentioned, i tested to see if i could change a variable and i could.
Checked the call, turns out the variable that holds the values i was sending in the TransferRow call was the problem, and was returning null, and so the values were null
Glad you fixed it. For next time though it's always easier for us if you include all the code. Then we can help. Partial information means more work for those trying to help you. We probably could have told you what the problem was straight away with all the info. Great job in working it out though man! Happy coding.