Prefab Camera - Variables Not Updating
I'm having issues with doing a camera shake and why this is not working is escaping me. There is a public method here called triggerShake that when an enemy dies this code is called - it sets a shake timer which should shake and deincrement. All of this works when dealing with scene objects, but when the camera/enemies are all instantiated prefabs - it doesn't work. It seems really strange, I can see all the code runs without error. The public method gets triggered when it's supposed to and I see in the debug.log the variable is set but the next update it's set back to 0. What else is strange is that if I set the initial shakeTime variable to something greater than zero, the camera does shake when I start it up. It's almost like it's treating the shakeTime variable in the public triggerShake method as a different variable entirely. In fact, I know that's what it's doing. If I set the code in triggerShake to += I see that variable increase each time it's triggered.
Not sure why the shakeTime initialized at the top is different than the shakeTime in triggerShake...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class screenShake : MonoBehaviour
{
private Transform tf;
private float shakeTime = 0.0f;
private float shakeMag = 0.05f;
private float dampingRate = 3.0f;
private Vector3 initialPosition;
// Update is called once per frame
void Update()
{
if (shakeTime > 0)
{
Debug.Log(shakeTime);
tf.localPosition = initialPosition + Random.insideUnitSphere * shakeMag;
shakeTime -= Time.deltaTime * dampingRate;
}
else
{
shakeTime = 0.0f;
tf.localPosition = initialPosition;
}
}
void Awake()
{
tf = GetComponent(typeof(Transform)) as Transform;
initialPosition = tf.localPosition;
}
public void triggerShake()
{
shakeTime = 5.5f;
Debug.Log(shakeTime);
}
}
Answer by bkeegan · Aug 03, 2019 at 09:32 PM
I was able to come up with a workable solution - although still not sure why I had to it this way. Since everything was working as items in the hierarchy and not as prefabs. I thought that maybe if a camera is instantiated from a prefab it behaves differently. Instead of calling the code from a reference prefab. I left the camera itself as a hierarchy item and just found that item by tag and called the triggerShake function that way
cams = GameObject.FindGameObjectsWithTag("MainCamera");
foreach (GameObject cam in cams)
{
cam.gameObject.GetComponent<screenShake>().triggerShake();
}
This works as intended, but again, still not sure why the variables wouldn't behave as intended when the camera was instantiated from a prefab.
Your answer
Follow this Question
Related Questions
How do I make a centipede/conga line out of game objects? 0 Answers
What does "Assertion failed on expression: 'prefab.IsPrefabParent()'" mean on my source data? 4 Answers
What does this do? Shader Language 1 Answer
Open Game Window programmatically : EditorWindow.GetWindow (typeof(Game)); 1 Answer
Help with Enemy IA walk-Attack-Retreat Jumping-wait-repeat 0 Answers