- Home /
Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't?
Hi,
I am working on a part of my project which makes a prefab instantiate in the character's hand when I drop it in a hotkey/slot. The script on my slot checks the tag of the image i have dropped in the slot and then tells the script on my character's hand which weapon it is so it knows which gun prefab to instantiate. My first attempt I was getting the exact same problem (return null when trying to contact script) but I realized it was because I had another hand model in my scene that had the same name as the hand of my character but no script. I changed the name of my chracter's hand and it started working perfectly. I saved my scene and project and went to play R6s for 20-30 mins. Came back to my PC and started looking through my gun prefabs in resources trying to decide what gun to put in the scene for scaling and stuff. For some reason Unity crashed when I clicked on one of the prefabs and after I rebooted Unity I am now getting the exact same problem but there is no gameobjects in the scene with the same names or anything else I can find that is causing the issue.
public GameObject PM40;
public void Equip () {
//Trying to get the script on hand (Hold_Rr)
WeaponChange weaponchange = GameObject.Find ("Hold_Rr").GetComponent<WeaponChange> ();
// Check the tag of image
if (SlotItem.gameObject.tag == "PM40") {
Debug.Log ("Tag PM40");
//Check if script is null
if (weaponchange == null) {
Debug.Log ("null");
return;
}
//Change GameObject to prefab of PM40 pistol
weaponchange.Holder1 = PM40;
//run the weapon change function to instantiate it
weaponchange.PistolHolder ();
}
}
This is the script attached to the hotkey. I checked to make sure it was finding the Hold_Rr object (hand) and it definitely is.
public GameObject Holder1;
public void PistolHolder () {
Debug.Log ("PistolHolder()");
GameObject gun = (GameObject)Instantiate(Holder1, transform.position, transform.rotation);
gun.transform.parent = pistolHolder.transform;
gun.transform.localRotation = Quaternion.identity;
gun.transform.localPosition = new Vector3 (0, 0, 0);
}
This is the function in the script on the Hold_Rr object that the other script is supposed to call after updating the Holder1 variable.
Something I noticed was the Hold_Rr object's name was in bold letters in the inspector and the Layer dropdown in bold letters as well. I reverted it and now it looks fine.
Please help me.. =(
I'm kinda having trouble following the script. I believe many problems can be solved yourself simply by using more descriptive names. I mean what is a pistol holder, is it a hand? And Holder1 is assigned a pistol model, why is it not called pistol$$anonymous$$odel? And you have a game object called "Hold_Rr", why is this not called rightHand or similar? I also think you should steer well clear of using function names that are the same as class/object names which can be seen on lines 2 & 7 of the hotkey script where you have a PistolHolder function and a pistolHolder instance.
Isn't it only bad to have functions and instances with the exact same names? In my script the function has a capital p and instance is lowercase. pistolHolder instance is from public GameObject pistolHolder; in the same script and is an empty object in the hand (Hold_Rr). I know the names are confusing I was going to start editing that but i'm busy trying to solve this first and it definitely is not causing any of the problems.
pistol holder is the empty object that is a child of the hand (Which is "Hold_Rr"). So in the Hold_Rr (Hand) there would be multiple empty objects for the different types of weaponds like pistolholder then primaryholder then meleeholder etc. It would be better to name pistolholder secondaryholder but the secondary is always going to be a pistol where as the primary could be shotguns, rifles etc. Everything in the PistolHolder function works fine though when it is called from the hotkey/slot script (The top script in my post) using weaponchange.PistolHolder();. The Holder1 is an empty GameObject in the WeaponChange script (bottom script) which is what will be instantiated in the emptyobject called pistolholder which is a child of "Hold_Rr" (the hand model) that has the WeaponChange script. It is just the part where the top script needs to tell the bottom script to make Holder1(which starts as empty) = the pistol prefab which is P$$anonymous$$40.
I tried this -
public WeaponChange weaponchange;
public void Equip () {
if (SlotItem.gameObject.tag == "P$$anonymous$$40") {
Debug.Log ("Tag P$$anonymous$$40");
if (weaponchange == null) {
Debug.Log ("null");
return;
}
weaponchange.Holder1 = P$$anonymous$$40;
weaponchange.PistolHolder ();
}
}
This way it worked because I made the WeaponChange public and just added it in the inspector so the script already knows what it is. This means for some reason it is not able to find the script component when trying to do it in the script and I don't understand why. Can you see anything wrong with just this part (Below) because that is what's returning null even though it should be able to find the WeaponChange script in the GameObject "Hold_Rr".
WeaponChange weaponchange = GameObject.Find ("Hold_Rr").GetComponent<WeaponChange> ();
weaponchange.Holder1 = P$$anonymous$$40;
Thanks.
Well without using a slot assigning P$$anonymous$$40 to Holder1 is assigning a null isn't it? You need to find and instantiate the prefab.
Answer by MezFo · Oct 21, 2017 at 06:33 AM
It must have been something to do with Unity crashing even though I saved. I just changed the name of the hand model from Hold_Rr to Hold_Rrr and changed it in the Find() function and now it works again. Changed it back to Hold_Rr and it still works. I'm guessing even though it said the hand was called Hold_Rr in the scene and inspector, Unity thought it was still "Hold_R" which is what it was called before I solved it the first time before it crashed... even though I saved.
Your answer
Follow this Question
Related Questions
How to reference an inactive GameObject? 2 Answers
NullReferenceException when trying to access other GO in script. C# 1 Answer
Can I use GetComponents reference outside Of awake and start functions 1 Answer
My player keeps sliding after I use my dash, help! 0 Answers
The spaceship acceleration script is good ? And how to use it with engine ? 0 Answers