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 MrFlibble · Apr 19, 2014 at 03:02 PM · raycastgunlinerendererlinelaser

Creating a Ray Gun

So I want to create a ray gun for my game using a Line Renderer, and although i thought I got the basic code right I see no laser.

Any help would be appreciated as I'm getting no error messages.

 using UnityEngine;
 using System.Collections;
 
 public class Laser2 : MonoBehaviour {
 
     public float shootTimer;
     public float coolDown;
 
     public float realoadtime = 10f;
     public int clip = 10;
     public int BulletsperClip = 1;
     public int Bulletsremaining = 10;
 
 
     public Color firstColour = Color.magenta;
     public Color secondColour = Color.red;
     public LineRenderer laser;
     public bool isOn;
     public float time;
     public float adj;
 
 
 
 
     // Use this for initialization
     void Start () {
 
         shootTimer = 0.15f;
         coolDown = 0.15f;
 
 
         LineRenderer  laser = (LineRenderer) gameObject.AddComponent("laser");
         laser.material = new Material (Shader.Find("Particles/Additive"));
         laser.SetWidth(0.05f, 0.05f);
         laser.SetVertexCount(2);
         laser.SetColors(firstColour, secondColour);
 
         isOn = false;
     
     }
     
     // Update is called once per frame
     
     void Update () {
         
         time += Time.deltaTime;
         
         if(Input.GetMouseButtonDown(0) && time > 0.5){ 
             
             time = 0.0f;
             
             
             var start = transform.position;
             laser.SetPosition(0,start);
         
             
 
             Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
             RaycastHit hit;
 
             if (Input.GetMouseButton (0) && shootTimer == 0 && Bulletsremaining > 0) {
 
             if(Physics.Raycast(ray, out hit, 25.0f))
             {
                 
                 laser.SetPosition(1, hit.point + hit.normal);
             }
 
 
             
             
             isOn = true;
             OnOff();
 
             }
             
         }
         
         
     }
 
 
 IEnumerator OnOff () {
     
     
     if (isOn == true); {
         yield return new WaitForSeconds (0.05f);
         isOn = false;
         laser.enabled = false;
     }
     
 }
 
     void OnGUI()
     {
         
         GUI.Box (new Rect (10, 50, 150, 20),"Ammo:" + Bulletsremaining + "/" + clip);
         
     }
 
 }
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 grahnzz · Apr 19, 2014 at 03:29 PM 0
Share

Do some debugging with your IDE and/or UnityEngine.Debug.Log()

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by grahnzz · Apr 19, 2014 at 03:32 PM

One thing that i see directly by looking at your code is that you are setting up a new instance of a line renderer in Start(), you probably intended to reference the variable laser that is global in the class.

 LineRenderer  laser = (LineRenderer) gameObject.AddComponent("laser");
 //try:
 laser = (LineRenderer) gameObject.AddComponent("laser");
Comment
Add comment · Show 3 · 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 MrFlibble · Apr 19, 2014 at 04:35 PM 0
Share

Thanks for the response but now I'm getting two new errors.

1: NullReferenceException: Object reference not set to an instance of an object Laser2.Start () (at Assets/Scripts/Laser2.cs:33)

and the second is the same but at line 54.

avatar image grahnzz · Apr 19, 2014 at 08:56 PM 0
Share

In your Start() where you initialize your LineRenderer: gameObject.AddComponent("laser") should probably be gameObject.AddComponent("LineRenderer")

avatar image robertbu · Apr 19, 2014 at 09:06 PM 0
Share

Taking a step further, you don't use a string version of AddComponent() if you can avoid it. Use this ins$$anonymous$$d:

    laser = gameObject.AddComponent<LineRenderer>();

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

21 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

Related Questions

LineRenderer. Problem with ray displaying 2 Answers

LineRenderer not working 0 Answers

How do I make a line render always be pointing at the center of the canvas/gui 1 Answer

How to get a LineRenderer to shoot from Gun Point to Mouse Position 1 Answer

Dynamically adjusting LineRenderer vertexes to follow raycasts 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