- Home /
Other
Targeting retical that changes color
I have this code for a targeting rectical that changes colors but im hiting the following errors.
ArgumentException: You are not allowed to call get_transform when declaring a variable. Move it to the line after without a variable declaration. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. Targeting..ctor () (at Assets/Targeting.js:6)
This is my code for it>
 var crosshairTexture : Texture2D;
 var crosshairRange = 200;
 
 private var hit : RaycastHit;
 private var facingDirection = transform.TransformDirection(Vector3(0,0,1));
  
 function Update () 
 {
     if (Physics.Raycast(transform.position, facingDirection, hit, crosshairRange))
     {
         if(hit.collider.gameObject.CompareTag("Untagged"))
         {
             if (hit.collider.gameObject.CompareTag("Enemy"))
             {
                 crosshairTexture.color = Color(1, 0, 0, 0.5);
             }
  
             else if (hit.collider.gameObject.CompareTag("Friend"))
             {
                 crosshairTexture.color = Color(0, 1, 0, 0.5);
             }
  
             else
             {
                 crosshairTexture.color = Color(1, 1, 1, 0.5);
             }
         }
     }
 }
Answer by Loius · Oct 27, 2012 at 04:11 AM
The error is pretty descriptive.
You can't `var x : Transform = transform.{anything}`
You need to initialize x inside a function (Start, Awake) instead.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Making my cross hair change colors 2 Answers
Object will not instantiate 1 Answer
Converting to C# 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                