- Home /
"Type mismatch" in play mode after doing member assignment
Hi!
In my program there is a script that adds to the model different kind of guns. It is made by creating custom editor script. So I instantiate in Editor mode a prefab. And then I run the game with guns attached. The problem is when I set the target to the first of guns the next gun already has that target due to what I am seeing in Debug mode(using breakpoints). I suppose this is not true however, because if you look in the editor, you will see that all instantiated prefabs have target == null and prefab itself has target == type mismatch.
What is that ?
Thank you!
Update:
Some code.. But there is nothing interesting in that..
// TurretControl.cs
public void SetTarget(GameObject go)
{
if (go == null)
Debug.Log("Bad");
Debug.Log("Before:" + target);
target = go;
Debug.Log("After:" + target);
}
The log looks like this(for 3 guns):
Before: null
After: someValue
Before: someValue
After: someValue
Before: someValue
After: someValue
I checked that "go" points to the correct target. However the prefab of gun will have target == type mismatch and the clones of that prefab (that should be changed) have target == null
Update2:
I created a simple project that represents the issue. Some description here:
Click on the prefab in the prefabs folder and see that target public variable is null. Switch into the play mode and right click on the cube. Watch again in the prefab target variable it should become "type mismatch" and GunPrefab(Clone) that is in Slot1 remains the same!
See attachment below. link text
There is nothing interesting in the code.. Just do the assignment.
What we want to see is, where the variable target
comes from. Is it just a member variable? Definitely some more information and types are needed.
Answer by Baste · Oct 29, 2014 at 10:36 PM
I found your bug!
To find it, follow these steps:
1: open your example project, click the Root object, and select "Set Weapons".
2: select the GunPrefab(Clone) in the Hireachy (the one that's instantiated), and ensure that the target variable of the GunControl script is set to nothing (null).
3: rename the GunPrefab(Clone) object to something else. I chose ThisWillShowTheError.
4: go to the GunControl script, and edit Update and SetTarget to be this:
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.D))
Debug.Log(this + " has target " + target);
}
public void SetTarget(GameObject go)
{
Debug.Log(this + "Sets target to: " + go);
target = go;
}
5: Go into play mode. First right click the cube, and then click 'D'.
Your output will be this:
GunPrefab (GunControl)Sets target to: Cube (UnityEngine.GameObject)
ThisWillShowTheError (GunControl) has target null
Do you get it?
If you haven't caught on yet: the FrontGun variable of the SomeScript script on the root object is pointing to the actual prefab, ins$$anonymous$$d of the instantiated copy of the prefab. So when this line happens:
if (frontGun) {
frontGun.GetComponent<GunControl>().SetTarget(go);
}
That frontGun is the prefab, not the actual object. The funny part here is that if you click Set Weapons after having clicked the cube, the prefab will be loaded with a reference to the cube. Fun times!
Baste, thank you very much for having time to look at the project! I've got what you said, however I am not sure I understand why that is happening ? Could you please explain it ? Thank you in advance!
So, there's two different versions of your frontGun. One is the one you place in your scene with the script, and the other one is the prefab. These are not the same object - the one in the scene is a clone of the prefab in your prefabs folder.
The input handling script has a reference to the prefab ins$$anonymous$$d of the clone. So when you're assigning the target, you're assigning it to the prefab, not the instanced clone in your scene.
Does that help?
Your answer
![](https://koobas.hobune.stream/wayback/20220613170241im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is it possible to prevent certain components from being used in AssetBundles? 0 Answers
How to Instantiate a GameObject from a ScriptableObject piece of script? 0 Answers
Using a Master Control Script w/Instantiated prefabs & multiple scenes? 0 Answers
How can I create and override Prefabs from a script? 0 Answers
Updating Pattern of Prefabs 1 Answer