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 retrohead · Oct 01, 2014 at 08:22 PM · c#instantiateprefabquaternion

problem spawning bullets in c#

i am currently working on the controls for my game. they will be similar to robotron,smash tv, or geometry. i have been trying to figure out for 2 days now how to instantiate my projectile prefab at the correct rotation and angle to match my right thumbstick input.

The prefab i am trying to spawn is placed here Assets/Prefabs/Projectile.

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
 
         Vector3 vNewInput = new Vector3(Input.GetAxis("Right Horizontal"), Input.GetAxis("Right Vertical"), 0.0f);
      
         if (vNewInput.sqrMagnitude < 0.1f)
                 {
 
                         return;
                }
         else
                {
             
                     
                        var direction = new Vector3 (Input.GetAxis ("Right Horizontal"), Input.GetAxis ("Right Vertical"), 0.0f);
                         var rotation = Quaternion.LookRotation (direction, Vector3.up);
                         transform.rotation = rotation;
 
                    
 
                         Instantiate (Projectile, direction, rotation);

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 Sabikku · Oct 01, 2014 at 08:31 PM 0
Share

What errors do you get? What's the effect of your pasted code?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by retrohead · Oct 02, 2014 at 12:41 AM

Here's the full code and errors I'm getting

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
     public Rigidbody projectile;
     public float speed = 0.2f;
     Animator animator;
     int dirInt;
     Vector3 v3;
     Vector3 shoot;
     // Use this for initialization
     void Start () {
         animator = this.GetComponent<Animator> ();
 
     }
 
 
 
     // Update is called once per frame
     void Update () {
 
 
         float vertical = Input.GetAxis ("Vertical");
         float horizontal = Input.GetAxis ("Horizontal");
 
         //This moves our player
          v3 = new Vector3(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical"),0);
         transform.Translate(speed * v3.normalized);
 
         //This Lets our player shoot
 
         
 
         Vector3 shootangle = new Vector3(Input.GetAxis("Right Horizontal"), Input.GetAxis("Right Vertical"), 0f);
          //Only do work if meaningful
         if (shootangle.sqrMagnitude < 0.1f)
             {
 
                     return;
             }
         else
             {
                
                        
                         var direction = new Vector3 (Input.GetAxis ("Right Horizontal"), Input.GetAxis ("Right Vertical"), 0.0f);
                         var rotation = Quaternion.LookRotation (direction, Vector3.up);
                         transform.rotation = rotation;
 
                       
 
                         Instantiate (Projectile , direction, rotation);
                        
             }
 
            
         //This controls the direction we face and which animation plays
         if (horizontal > 0) {
                        
                         animator.SetInteger ("Direction", 1);
                         dirInt = 1;
                 } else if (horizontal < 0) {
                                    
                         animator.SetInteger ("Direction", 3);
                         dirInt = 3;
                 } else if (vertical > 0) {
                        
                         animator.SetInteger ("Direction", 2);
                         dirInt = 2;
                 } else if (vertical < 0) {
                        
                         animator.SetInteger ("Direction", 0);
                         dirInt = 0;
                 } else if (horizontal > 0 && vertical > 0) {
                        
                         animator.SetInteger ("Direction", 1);
                         dirInt = 1;
                 }
         else if(horizontal == 0)
         {
             if(dirInt == 1)
             {
                 animator.SetInteger("Direction",5);
             }
             if(dirInt == 2)
             {
                 animator.SetInteger("Direction",6);
             }
             if(dirInt == 3)
             {
                 animator.SetInteger("Direction",7);
             }
             if(dirInt == 0)
             {
                 animator.SetInteger("Direction",4);
             }
         }
    
     }
 }


Assets/Scripts/Player.cs(52,62): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

Assets/Scripts/Player.cs(52,49): error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments Assets/Scripts/Player.cs(52,49): error CS1503: Argument #1' cannot convert object' expression to type UnityEngine.Object'

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 revolute · Oct 02, 2014 at 02:05 AM 0
Share

You have a script named Projectile right? C# is case sensitive. In other words, 'P' should be 'p'

avatar image
0

Answer by OboShape · Oct 02, 2014 at 10:19 AM

you have declared projectile on Line 6

public Rigidbody projectile;

however on line 52 you are instantiating Projectile prefab.

Instantiate (Projectile , direction, rotation);

as revolute says C# is case sensitive.

ensure that all entries to your projectile are of the same case. amment line 52 to read..

Instantiate (projectile , direction, rotation);

See how that goes :)

Comment
Add comment · Show 2 · 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 retrohead · Oct 02, 2014 at 01:17 PM 0
Share

thank your all for you replys, after changing Projectile to projectile my error now comes while the game is running and i atempt to shoot. i get this error...

UnassignedReferenceException: The variable projectile of Player has not been assigned. You probably need to assign the projectile variable of the Player script in the inspector. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:74) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:84) Player.Update () (at Assets/Scripts/Player.cs:52)

it appears to be looking on my player game object for the rigidbody variable, which is the gameobject with the above script attatched. i am unsure how to reference my prefab ins$$anonymous$$d from the player script

avatar image retrohead · Oct 02, 2014 at 01:41 PM 0
Share

never$$anonymous$$d, i understand now. this absolutely fixed my problem. it made a variable in the inspector on my player gameobject i could attach my prefab too. thank you very much for your help.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiate prefab (button, texture) with attached script 1 Answer

Having multiple objects fire prefabs in different times C# 0 Answers

Object after instantiate doesn't apply AddForce 1 Answer

instantiate prefab assigned in inspector c# 1 Answer

Clones are still in the hierarchy after exiting play mode 2 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