Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by glad · Oct 29, 2014 at 12:31 PM · prefabscustom editorinstantiation

"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

new unity project 2.zip (156.1 kB)
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Oct 29, 2014 at 12:33 PM 0
Share

We need to see code to understand this.

avatar image glad · Oct 29, 2014 at 01:02 PM 0
Share

There is nothing interesting in the code.. Just do the assignment.

avatar image Unitraxx · Oct 29, 2014 at 01:24 PM 0
Share

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.

avatar image glad · Oct 29, 2014 at 02:44 PM 0
Share

Created a project that represents the issue

avatar image glad · Oct 29, 2014 at 08:04 PM 0
Share

I am not sure.. But may be this is a Unity bug ?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baste · Oct 29, 2014 at 10:38 PM 0
Share

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!

avatar image glad · Oct 30, 2014 at 08:33 AM 0
Share

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!

avatar image Baste · Oct 30, 2014 at 09:16 AM 1
Share

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?

avatar image glad · Oct 30, 2014 at 09:51 AM 0
Share

Yes, thank you so much!!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges