- Home /
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);
         
     }
 
 }
Do some debugging with your IDE and/or UnityEngine.Debug.Log()
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");
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.
In your Start() where you initialize your LineRenderer: gameObject.AddComponent("laser") should probably be gameObject.AddComponent("LineRenderer")
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
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                