• 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
Question by Amir aba · Nov 23, 2013 at 02:51 AM ·

Simple bullet script

I am trying to make a simple bullet script that will spawn in a bullet(the bullet itself will have the script that does damage), but I am getting an error.

using UnityEngine; using System.Collections;

public class shooting : MonoBehaviour {

 public float bulletSpeed = 10;
 
 
 
 void Fire() 
 {
 Rigidbody bulletClone = (Rigidbody) Instantiate(bullet, transform.position, transform.rotation);
 bulletClone.velocity = transform.forward * bulletSpeed;

 // You can also acccess other components / scripts of the clone
 //rocketClone.GetComponent<MyRocketScript>().DoSomething();
 }
 
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetButtonDown("Fire1"))
         Fire(); 
 
 }

}

it is telling be that bullet does not exist in this context; there is a bullet in my prefabs, so I am not really sure what is going on.

Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by clunk47 · Nov 23, 2013 at 02:55 AM

'bullet does not exists in the current context', because you don't have a 'bullet' variable. You need a variable to reference the prefab. Easiest way is to make a public variable so you can drag and drop the prefab onto your scripted object's 'shooting' component in the inspector:

 using UnityEngine; 
 using System.Collections;
 
 public class shooting : MonoBehaviour 
 {
     public float bulletSpeed = 10;
     public Rigidbody bullet;
     
     
     void Fire()
     {
         Rigidbody bulletClone = (Rigidbody) Instantiate(bullet, transform.position, transform.rotation);
         bulletClone.velocity = transform.forward * bulletSpeed;
     }
 
     void Update () 
     {
         if (Input.GetButtonDown("Fire1"))
             Fire();
     }
 }
Comment
Dismortus
PrairieDogSeeksHeart
MalivoCZE
anirudhbhardwaj48
hamanieye

People who like this

5 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 connorwforman · Jun 28, 2017 at 12:27 PM 0
Share

For the public float variable, you need to add an 'f' after the 10, otherwise the script will not work correctly. All float variables with a number attached need to have an 'f' after them.

avatar image

Answer by Kzapas · Jan 28, 2020 at 11:01 PM

Here is my insanely, way too complicated bullet script. But hey, it works for my game!

 public float delaytoDestroy = 10;
     public float speed;
     private GameObject menu;
     public float delaydeath = 10;
     private GameObject FPS;
     private GameObject PauseManager;
     private GameObject Player;
     private GameObject PauseCamera;
     bool cursorLocked = false;
     
     //Find Inactive GameObjects
     GameObject FindInActiveObjectByName(string name)
     {
         Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
         for (int i = 0; i < objs.Length; i++)
         {
             if (objs[i].hideFlags == HideFlags.None)
             {
                 if (objs[i].name == name)
                 {
                     return objs[i].gameObject;
                 }
             }
         }
         return null;
     }
 
     //Assign gameobjects to inactive current scene objects
     void Start()
     {
         menu = FindInActiveObjectByName("DeathMenu");
         FPS = FindInActiveObjectByName("FPS");
         PauseManager = FindInActiveObjectByName("PauseManager");
         Player = FindInActiveObjectByName("Player");
         PauseCamera = FindInActiveObjectByName("PauseCamera");
     }
 
 
     void Update()
     {
         transform.position += transform.forward * Time.deltaTime * speed;
 
         StartCoroutine(Wait(delaytoDestroy));
         IEnumerator Wait(float delay)
         {
 
 
             yield return new WaitForSeconds(delay);
             {
                 Destroy(gameObject);
             }
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         //Destroy
 
         StartCoroutine(Wait(delaytoDestroy));
         IEnumerator Wait(float delay)
         {
 
 
             yield return new WaitForSeconds(delay);
             {
                 Destroy(gameObject);
                 if (other.CompareTag("Player"))
                 {
                     StopCoroutine("Wait");
                 }
             }
         }
         
             //Death
 
             StartCoroutine(WaitFirst(delaydeath));
         IEnumerator WaitFirst(float delay)
         {
 
 
             yield return new WaitForSeconds(delay);
             if (other.CompareTag("Player"))
             {
                 menu.SetActive(true);
             }
         }
 
         StartCoroutine(Wait2(delaydeath));
         IEnumerator Wait2(float delay)
         {
 
 
             yield return new WaitForSeconds(delay);
             if (other.CompareTag("Player"))
             {
 
                 FPS.SetActive(false);
             }
         }
 
         StartCoroutine(Wait3(delaydeath));
         IEnumerator Wait3(float delay)
         {
 
 
             yield return new WaitForSeconds(delay);
             if (other.CompareTag("Player"))
             {
 
                 PauseManager.SetActive(false);
             }
         }
 
         StartCoroutine(Wait4(delaydeath));
         IEnumerator Wait4(float delay)
         {
 
 
             yield return new WaitForSeconds(delay);
             if (other.CompareTag("Player"))
             {
 
                 Player.SetActive(false);
             }
         }
 
         StartCoroutine(Wait5(delaydeath));
         IEnumerator Wait5(float delay)
         {
 
 
             yield return new WaitForSeconds(delay);
             if (other.CompareTag("Player"))
             {
 
                 PauseCamera.SetActive(true);
             }
         }
         StartCoroutine(Wait7(delaydeath));
         IEnumerator Wait7(float delay)
         {
 
 
             yield return new WaitForSeconds(delay);
             if (other.CompareTag("Player"))
             {
                 cursorLocked = !cursorLocked;
             }
         }
         StartCoroutine(Wait6(delaydeath));
         IEnumerator Wait6(float delay)
         {
 
 
             yield return new WaitForSeconds(delay);
             if (other.CompareTag("Player"))
             {
                 if (cursorLocked)
                 {
                     Cursor.lockState = CursorLockMode.None;
                     Cursor.lockState = CursorLockMode.Confined;
                     Cursor.visible = true;
                 }
             }
         }
 
 
     }
 
 }
 
 
 
Comment
hamanieye

People who like this

1 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How to Instantiate a prefab onto a grid? 1 Answer

Spawning randomly in a location I want 1 Answer

Spawning platform when i press a button 1 Answer

Create a streched cube between any two selected points. 1 Answer

Display GUI on mouse click 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