Referencing MainCamera localPosition

Hello,

i’ve found a simple script for shaking the camera, here’s the code, nothing complex:

using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour
{
	// Transform of the camera to shake. Grabs the gameObject's transform
	// if null.
	public Transform camTransform;
	
	// How long the object should shake for.
	public float shakeDuration = 0f;
	
	// Amplitude of the shake. A larger value shakes the camera harder.
	public float shakeAmount = 0.7f;
	public float shakeAmountDecrease = 1.0f;
	public float decreaseFactor = 1.0f;
	
	Vector3 originalPos;
	
	void Awake()
	{
		if (camTransform == null)
		{
			camTransform = GetComponent(typeof(Transform)) as Transform;
		}
	}
	
	void OnEnable()
	{
		originalPos = camTransform.localPosition;
	}

	void Update()
	{
		if (shakeDuration > 0)
		{
			camTransform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
			
			shakeDuration -= Time.deltaTime * decreaseFactor;
		}
		else
		{
			shakeDuration = 0f;
			camTransform.localPosition = originalPos;
		}
		
		if (shakeAmount > 0)
		{
		
		shakeAmount -= Time.deltaTime * shakeAmountDecrease;
		
		}	
		
		else
		{
			
		shakeAmount = 0f;
		camTransform.localPosition = originalPos;
		}
		
	}
}

Since it will be activated on enemy despawn, i replaced the awake() method with the usual find game object/get component stuff, so basically, my camTransform is now camera:

GameObject MainCamera = GameObject.Find("MainCamera");
Camera camera = MainCamera.GetComponent<Camera>();

BUT

something happens. Unity asks me if i made a backup and changes my script. Everywhere there was camera, now’s GetComponent().

Now it looks like this, and it doesn’t work

using UnityEngine;
using System.Collections;

public class CameraShake2 : MonoBehaviour
{
	// Transform of the camera to shake. Grabs the gameObject's transform
	// if null.

	// How long the object should shake for.
	public float shakeDuration = 0f;
	public float decreaseFactor = 1.0f;
	
	// Amplitude of the shake. A larger value shakes the camera harder.
	public float shakeAmount = 0.7f;
	public float shakeAmountDecrease = 1.0f;
	
	
	Vector3 originalPos;
	
	void Awake()
	
	{
		GameObject MainCamera = GameObject.Find("MainCamera");
		Camera camera = MainCamera.GetComponent<Camera>();
	}
			
	void OnEnable()
	
	{
		originalPos = GetComponent<Camera>().localPosition;
	}

	void Update()
	
	{	
	if (shakeDuration > 0)
	
	{
			GetComponent<Camera>().localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
			
			shakeDuration -= Time.deltaTime * decreaseFactor;
		}
		else
		{
			shakeDuration = 0f;
			GetComponent<Camera>().localPosition = originalPos;
		}
		
		if (shakeAmount > 0)
		{
		
		shakeAmount -= Time.deltaTime * shakeAmountDecrease;
		
		}	
		
		else
		{
			
		shakeAmount = 0f;
		GetComponent<Camera>().localPosition = originalPos;
		}
		
	}
}

The error i get is the following: Type UnityEngine.Camera' does not contain a definition for localPosition’ and no extension method localPosition' of type UnityEngine.Camera’ could be found (are you missing a using directive or an assembly reference?)

I don’t get it :frowning:

You need the transform that comes with the camera, now you are using the camera component itself which is not a transform. Use Camera.main.transform to get the transform of the main camera of your project or just use Camera.main.transform.localPosition = …