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 Fender · Dec 31, 2014 at 03:04 AM · errorgameobjectprefabrigidbody.velocity

How can I spawn a bullet using PhotonNetwork.Instantiate()?

I am trying to spawn a bullet/projectile by using PhotonNetwork.Instantiate(). I have attached this code to the Main Camera:

 using UnityEngine;
 using System.Collections;
 
 public class Ability1 : MonoBehaviour {
     public GameObject projectile;
     public float projectilespeed = 20;
     void Update(){
         if (Input.GetButtonDown ("Fire1")) {
             Rigidbody clone =(Rigidbody)PhotonNetwork.Instantiate(projectile, transform.position, transform.rotation, 0);
                     clone.velocity = transform.TransformDirection(Vector3.forward * projectilespeed);
             Destroy(clone.gameObject, 3);
         }
     }
 }
 

I have referenced the "Bullet" prefab by dragging it into the projectile box. My project is structured like this: Assets>Player>Character 1>Resources>Bullet. It is also worth noting that I have another prefab in the same folder as "Bullet" called "Character 1". I get 3 errors when I run this code:

  1. NullReferenceException: Object reference not set to an instance of an object

  2. The best overloaded method match for PhotonNetwork.Instantiate(string, UnityEngine.Vector3, UnityEngine.Quaternion, int)' has some invalid arguments 3. Argument #1' cannot convert UnityEngine.GameObject' expression to type string'

I have experimented and tinkered with the code and cant find a solution. Does anyone know what the problem is and how to fix it? Thank you in advanced.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kevinspawner · Dec 31, 2014 at 04:26 AM

Rigidbody clone =(Rigidbody)PhotonNetwork.Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;

Not tested try this.

Comment
Add comment · Show 1 · 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 Fender · Dec 31, 2014 at 04:43 PM 0
Share

It didnt work, but I found a workaround by typing

 Gameobject clone = (Gameobject)PhotonNetwork.Instantiate("projectile", transform.position, transform.rotation);
 clone.rigidbody.velocity = Vector3.forward * projectilespeed;

Turns out you cant pass a variable into the first parameter of PhotonNetwork.Instatiate(). I have another issue though. Now, when I spawn a projectile, it does not face my current rotation.For example, I will turn my character to the left or right, but the bullets always travel along the z- axis. They should follow my rotation, like bullets co$$anonymous$$g out of the barrel of the gun that I am holding. Do you know how to fix this?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

What does "Couldn't find matching instance in prefab" mean? 2 Answers

Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer

"Prefab GameObject's can not be made active!" - Need a fix 1 Answer

InvalidCastException when Instantiating with photon (PUN) 2 Answers

Convert code to get prefab then sprite ? 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