Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Dazzyreil · Jun 17, 2015 at 03:21 AM · camerashake

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;
         }
     }
 }
Comment
Add comment · Show 2
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
avatar image robbagus · Jun 17, 2015 at 05:02 AM 0
Share

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.

avatar image lengzai96 · Jun 17, 2015 at 05:27 AM 0
Share

@robbagus Your opinion is wrong

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;

Comment
Add comment · Show 1 · 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
avatar image Dazzyreil · Jun 17, 2015 at 03:24 PM 0
Share

Thanks this worked!

avatar image
0

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.

Comment
Add comment · Show 1 · 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
avatar image Dazzyreil · Jun 17, 2015 at 03:25 PM 0
Share

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

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

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


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