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 TheFracker · Apr 12, 2014 at 10:32 PM · instantiateprefabargumentexception

Can't instantiate bullets when calling shoot function in one script from another script.

Hello,

I have a problem that I have been struggeling with for too long, so I hope that you can come to my aid.

I am making a game with some friends and we are using Unity in 2D mode. We have a Weapon class that can shoot, and then we have an Enemy class that should use the weapon to shoot, so it calls the shoot() in Weapon, but for some reason it will not instantiate the bullets when doing it this way.

Class Shooter: The point of this class is to call pew() in the PewPew class.

 public class Shooter : MonoBehaviour {

 PewPew pewpew;

 // Use this for initialization
 void Start () {
     pewpew = gameObject.AddComponent<PewPew> ();
 }
 
 // Update is called once per frame
 void Update () {
     pewpew.pew ();
 }

}

Class PewPew: This class is the one that will instantiate the bullets and send them on their way, and it does do so, if I call pew() inside this class but when I call pew() from Shooter all I get is a bounch of errors.

 public class PewPew : MonoBehaviour {

 public GameObject bullet;
 float speed = 20.0f;


 // Use this for initialization
 void Start () {
 }
 
 // Update is called once per frame
 void Update () {
     if(!true) // If set to true, it will start spawning bullets
     {
         pew ();
     }
 }

 public void pew()
 {
     Debug.Log ("PEWPEW");
     Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
     bulletInstance.velocity = new Vector2(speed, 0);
 }

}

I get the Debug.Log message whenever I call the function, so I know that I can get to it, but for some reason I just can't get it to instantiate the bullets when I call from Shooter.

Thank you in advance for any help you can provide.

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 KMKxJOEY1 · Apr 14, 2014 at 01:08 AM

Ok first of all, in your Shooter class, this code is redundant if you already have PewPew attached to the GameObject:

 // Use this for initialization
 void Start () {
     pewpew = gameObject.AddComponent<PewPew> ();
 }

If not, then you are just adding a second PewPew instance onto your shooter object. I would recommend deleting that line, and just assigning the PewPew class to the variable in the inspector.

Also, this code could be simplified from

 Quaternion.Euler(new Vector3(0,0,0))

to simply:

 Quaternion.identity
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

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

22 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

Related Questions

Some Prefabs are not Loading 1 Answer

ArgumentException: The prefab you want to instantiate is null. 1 Answer

ArgumentException: The prefab you want to instantiate is null. 1 Answer

Updating a variable on a script in an instanced object 1 Answer

Why is transform.position of a prefab that's been instantiated different than a gameobject created on the scene, when they are in the exact same position? 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