- Home /
Camera shake scripts works but camera wont stay in prefab
Hi guys,
So I found a camera shake script that works perfectly for me, kinda. This script is attached to an explosion that gets instantiated whenever a bomb explodes. When I add the main camera to the public transform field it does not stay whenever I turn it into a prefab, the field is just empty.
Is there a reason for this or is it a bug?
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 shake = 0f;
// Amplitude of the shake. A larger value shakes the camera harder.
public float shakeAmount = 0.7f;
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 (shake > 0)
{
camTransform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
shake -= Time.deltaTime * decreaseFactor;
}
else
{
shake = 0f;
camTransform.localPosition = originalPos;
}
}
}
It's not a bug, that's just how unity works. They can't find the camera cause it's not part of the prefab. Attach the $$anonymous$$ain Camera to your Parent object as a Child and it should stay attached while being converted to a prefab.
Edit: Alternate solution will be making your $$anonymous$$ain Camera a prefab and use that camera prefab in your script ins$$anonymous$$d of the one in hierarchy.
Answer by duck · Jun 17, 2015 at 08:34 AM
A prefab can't contain references to objects in a scene. This is because a prefab can be used in any/multiple scenes - therefore references to items in a specific scene would not make sense, because that scene might not be the one that is loaded.
The simplest way to access the camera is to make sure your camera is tagged MainCamera (the initial camera in the scene has this tag by default).
You can then access the main camera via script using Camera.main
So, on line 23, instead of
camTransform = GetComponent(typeof(Transform)) as Transform;
which by the way, is a bizarrely convoluted way of writing: camTransform = transform;
You could have:
camTransform = Camera.main.transform;
Answer by smallbit · Jun 17, 2015 at 05:08 AM
Prefab cannot hold reference to gameobject or any of its components class from the scene, because it is not a part of the scene (before instantiating). Your best shot is to find it on the run just after instantiating explosion by either GameObject.Find (slow) or direct access to the object via singleton (fast).
Alternatively you could change your camera into prefab too and see if it works.
I tried turning the camera into a prefab but that also didn't work, anyway thanks for taking the time to answer! Luckily someone posted a solution, which was very simple :)
Your answer
Follow this Question
Related Questions
Is it possible to shake the screen rather than shake the camera? 3 Answers
Camera shaking (problem with rigidbodyfpscontroller) 0 Answers
Camera shake, everything disappear from screen except canvas. 3 Answers
How to combine the camera that follows the player and the camera shake effect in Unity? 2 Answers
How to make camera position relative to a specific target. 1 Answer