- Home /
This question was
closed Jan 04, 2015 at 04:55 PM by
Graham-Dunnett for the following reason:
Duplicate Question
Question by
BitGlitch · Jan 04, 2015 at 04:55 PM ·
raycastingraycasthitcs1061
Error with raycast
Hello, In one of my scripts unity is telling me:
Assets/Scripts/Weapons/Laser.cs(76,37): error CS1061: Type UnityEngine.RaycastHit[]' does not contain a definition for transform' and no extension method transform' of type UnityEngine.RaycastHit[]' could be found (are you missing a using directive or an assembly reference?)
and here is my code:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(LineRenderer))]
public class Laser : MonoBehaviour {
public float laserWidth = 1.0f;
public int damage = 5;
public float noise = 0f;
public float maxRange = 10.0f;
public Color color = Color.green;
LineRenderer lineRenderer;
int length;
Vector3[] position;
//Cache any transforms here
Transform myTransform;
Transform endEffectTransform;
//The particle system, in this case sparks which will be created by the Laser
public ParticleSystem endEffect;
Vector3 offset;
// Use this for initialization
void Start () {
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.SetWidth(laserWidth, laserWidth);
myTransform = transform;
offset = new Vector3(0,0,0);
endEffect = GetComponentInChildren<ParticleSystem>();
if(endEffect)
endEffectTransform = endEffect.transform;
}
// Update is called once per frame
void Update () {
RenderLaser();
}
void RenderLaser(){
//Shoot our laserbeam forwards!
UpdateLength();
lineRenderer.SetColors(color,color);
//Move through the Array
for(int i = 0; i<length; i++){
//Set the position here to the current location and project it in the forward direction of the object it is attached to
offset.x =myTransform.position.x+i*myTransform.forward.x+Random.Range(-noise,noise);
offset.y =myTransform.position.y+i*myTransform.forward.y+Random.Range(-noise,noise);
offset.z =i*myTransform.forward.z+Random.Range(-noise,noise)+myTransform.position.z;
position[i] = offset;
position[0] = myTransform.position;
lineRenderer.SetPosition(i, position[i]);
}
}
void UpdateLength(){
//Raycast from the location of the cube forwards
RaycastHit[] hit;
hit = Physics.RaycastAll(myTransform.position, myTransform.forward, maxRange);
int i = 0;
while(i < hit.Length){
//Checks if the laser hits a mech's center collider
if(hit[i].collider.tag == "Mech Center")
{
hit.transform.SendMessageUpwards("ApplyDamageCenter", damage);
}
//Checks if the laser hits a mech's right arm collider
if(hit[i].collider.tag == "Mech Right Arm")
{
hit.transform.SendMessageUpwards("ApplyDamageRightArm", damage);
}
//Checks if the laser hits a mech's left arm collider
if(hit[i].collider.tag == "Mech Left Arm")
{
hit.transform.SendMessageUpwards("ApplyDamageLeftArm", damage);
}
//Check to make sure we aren't hitting triggers but colliders
if(!hit[i].collider.isTrigger)
{
length = (int)Mathf.Round(hit[i].distance)+2;
position = new Vector3[length];
//Move our End Effect particle system to the hit point and start playing it
if(endEffect){
endEffectTransform.position = hit[i].point;
if(!endEffect.isPlaying)
endEffect.Play();
}
lineRenderer.SetVertexCount(length);
return;
}
i++;
}
//If we're not hitting anything, don't play the particle effects
if(endEffect){
if(endEffect.isPlaying)
endEffect.Stop();
}
length = (int)maxRange;
position = new Vector3[length];
lineRenderer.SetVertexCount(length);
}
}
Does anyone see what is wrong?
Comment