• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by bkeegan · Aug 03, 2019 at 07:48 PM · programmingprefabs

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);
     }
 
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

192 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

For some reason I cannot drag my prefabs from the Project folder into the scene hierarchy. 2 Answers

How to redraw a texture on a prefab instance when a property in the inspector is changed by the developer? 0 Answers

Limit an amount of prefabs instantiated in javascript for Unity 5. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges