- Home /
Rotation Issues error CS1612:,Can't edit rotation (error CS1612)
Im trying to make a simple AI for my game but for some reason I keep getting this error:
error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.rotation'. Consider storing the value in a temporary variable.
Here's my code:
     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class livingAI : MonoBehaviour {
     
         public Transform AI;
     
         public void FixedUpdate ()
         {
             AI.rotation.y = AI.rotation.y + Random.Range (-45, 45);
             AI.transform.Translate (-5, 0, 0);
         }
     }
I've already tried multiple solutions i found on this forum, but none of them worked.
Answer by Cornelis-de-Jager · Apr 05, 2018 at 01:49 AM
You can't change a single value of rotation (a Qauternion). You have to assign a new rotation entirely. Also Quaternions don't use + like Vectors, if you want to add to one you need to use *. See example below:
 using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class livingAI : MonoBehaviour {
      
          public Transform AI;
      
          public void FixedUpdate ()
          {
             Quaternion rotation = new Quaternion.Euler(0, Random.Range (-45, 45), 0);
             AI.rotation.y = AI.rotation * rotation;
             AI.transform.Translate (-5, 0, 0);
          }
      }
So Just to re-itterate on your mistake:
Wrong:
AI.rotation.y = Random.Range(-45,45)
Right:
AI.rotation = new Quaternion(AI.rotation.x,Random.Range(-45,45), AI.rotation.z, AI.rotation.w); 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                