Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Salmjak · Feb 15, 2016 at 02:19 PM · c#networkingspawn

UNet - The thing you want to instantiate is null

I try to instantiate an object in the game. It works perfectly for the host and other players can see it. For clients I just get error "The thing you want to instantiate is null". This script is attached to the Player gameObject with local-player network identity. The relevant code:

 public void ActivateSkill(string skillName)
 {
     if (!isLocalPlayer)
     {
         return;
     }
     var command = (SkillDesc)skillDatabase.skills.Find(x => x.displayName == skillName);
     ProcessCommand(command);
 }  [Command]
     void CmdSpawnEffect(GameObject effect, float size, Vector3 pos)
     {
         GameObject obj = (GameObject)Instantiate(effect, pos, Quaternion.identity);
         obj.transform.localScale = effect.transform.localScale * size;
         Destroy(obj, 5f);
         NetworkServer.Spawn(obj);
     }
 
     private void ProcessCommand(SkillDesc command)
     {
         foreach (var fx in command.VisualEffects)
         {
             switch (command.placement)
             {
                 case SkillDesc.EffectPlacement.CenteredOnCharacter:
                     break;
                 case SkillDesc.EffectPlacement.CenteredOnFirstTarget:
                     break;
                 case SkillDesc.EffectPlacement.CenteredOnMouse:
                     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                     RaycastHit hit;
                     if (Physics.Raycast(ray, out hit, float.PositiveInfinity, myLayerMask))
                     {
                         Vector3 pos = hit.point;
                         pos.y += 0.2f;
                         CmdSpawnEffect(fx.Particle_effect, fx.size, pos);
                     }
                     break;
                 case SkillDesc.EffectPlacement.CenteredOnTargets:
                     break;
             }


Again, this works as intended on the server-client EDIT: I should add: The GameObject is retrieved from a ScriptableObject. SkillDatabase is a scriptableobject with a list skills containing the ScriptableObject skill. The scriptableobject skill contains the prefab. EDIT #2: The skilldatabase is instantiated on run-time, does that mean I have to register all the skills and their prefabs on run-time? Is that it? EDIT #3: Nvm, that didn't do shit. if (isClient) { for (int i = 0; i < skillDatabase.skills.Count; i++) { foreach (var obj in skillDatabase.skills[i].VisualEffects) { ClientScene.RegisterPrefab(obj.Particle_effect); } } }

Comment
Add comment · Show 1
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 meat5000 ♦ · May 11, 2016 at 09:13 AM 0
Share

Prefabs and object to be spawned must be registered.

You can do this in Network$$anonymous$$anager in editor or ClientScene by script.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Salmjak · Feb 15, 2016 at 10:59 AM

I solved it by sending the string name of the prefab to the [Command] and then using Resources.Load(string) to instantiate it.

 [Command]
     void CmdSpawnEffect(string effect, Vector3 pos, float size, float duration)
     {
         GameObject obj = (GameObject)Instantiate(Resources.Load(effect, typeof(GameObject)), pos, Quaternion.identity);
         Destroy(obj, duration);
         NetworkServer.Spawn(obj);
     }

I would appreciate input on why my original method doesn't work.

Comment
Add comment · 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
0

Answer by BackslashOllie · May 07, 2016 at 08:02 AM

The reason your first method does not work is [Command] functions will only accept primitive parameters (like int, float, string).You cannot pass a GameObject to the host. So I think your string loading method is probably the better solution.

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 meat5000 ♦ · May 07, 2016 at 09:26 AM 0
Share

Actually, you can, but only GameObjects with a Network Identity.

Arguments to Remote Actions

The arguments passed to commands and ClientRpc calls are serialized and sent over the network. These arguments can be:

  • basic types (byte, int, float, string, UInt64, etc)

  • arrays of basic types structs containing allowable types

  • built-in unity math types (Vector3, Quaternion, etc)

  • NetworkIdentity

  • NetworkInstanceId

  • NetworkHash128

  • GameObject with a NetworkIdentity component attached

Arguments to remote actions cannot be subcomponents of GameObjects, such as script instances or Transforms. They cannot be other types that cannot be serialized across the network.

http://docs.unity3d.com/$$anonymous$$anual/UNetActions.html

avatar image BackslashOllie meat5000 ♦ · May 07, 2016 at 05:25 PM 0
Share

So what are your thoughts on the original posters question? I have tested similar behaviour and I get the same result when trying to pass the prefab as a parameter to the [Command]. When explicitly using the variable within the method the error is not thrown.

avatar image meat5000 ♦ BackslashOllie · May 11, 2016 at 04:30 PM 0
Share

Remember, GOs with NI.

Show more comments

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

103 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 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 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 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

Players not moving properly - Network Multiplayer 1 Answer

How to to spawn player, according to id? 0 Answers

Netcode [Command] Issues .. no errors, just not working. 1 Answer

Multiple Cars not working 1 Answer

Spawning too many Objects when loading the World (k_eresultlimitexceeded) 0 Answers


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