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 Max 4 · Apr 19, 2011 at 10:50 AM · cameradestroy

Destroying the object but leaving the camera

I have a game where the camera is locked onto the player. The player is destroyed upon impact with an object, and explodes. However, the game crashes as the camera is also destroyed. Is there any way I could leave the camera to view the explosion?

Comment
Add comment · Show 1
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 poncho · Apr 19, 2011 at 01:16 PM 0
Share

it would be really of help if you say your code to the destroy, wich class you use to call it and wich method is in the destruction

3 Replies

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

Answer by Owen-Reynolds · Apr 19, 2011 at 01:52 PM

Destroying an object destroys all the kids. That's a really nice feature (killing a bullet also kills the con-trail, for example.) In your case, it destroys the camera. The real solution is you will naturally add a camera script, eventually. and will unchild the camera. If you don't want to do that yet, try:

// player killing itself code:
Camera.main.transform.parent=null; // unchild from player. Keeps same position in world
Destroy(gameObject); // player kills itself

If you spawn a new player, you'll need to reposition the camera:

newPlayer = Instantiate(blah, blah...) Camera.main.transform.parent = newPlayer; // locked onto new player // keeps old pos and facing (where last guy died,) but will now move with player

// No way camera can know to snap to correct position, so make it: Camera.main.transform = new Vector3(0,4,-10); // Untested. since cam is now a child, puts it relative to player: up and behind

Camera.main.transform.LookAt(newPlayer); // Fix the angle. Camera should stay "locked" at this relative angle

Comment
Add comment · Show 4 · 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 Max 4 · Apr 20, 2011 at 06:45 AM 0
Share

It says "Assets/aeroplane/crash.js(6,21): BCE0019: 'parent' is not a member of 'UnityEngine.Camera'."

avatar image Owen-Reynolds · Apr 20, 2011 at 08:05 PM 0
Share

Right...Camera is only a Component, a part of something. You look at the script reference, and Camera's don't have parents. But, we want to find the parent. What does have a parent? Well, the actual camera, in the Inspector, hangs off a transform (everything deos,) and Camera.transform is legal, and transforms do have parents. Since 2 checked me, editting answer.

avatar image Max 4 · Apr 21, 2011 at 12:32 AM 0
Share

It still crashes. The explosion begins, and then the game stops running.

avatar image Owen-Reynolds · Apr 21, 2011 at 02:39 AM 0
Share

Try editing your Q with details. Uh...no camera code, childed to player(?) Show the code that destroys you, does it give any errors? Does it freeze? All Sky? Drop out of Play mode? If you unchild the camera and put it in the sky, does it still break? etc...

avatar image
0

Answer by · Apr 19, 2011 at 10:52 AM

Add an ifcheck to your camera code, so it will only track the player if they exist.

Pseudocode:

if ( player != null )
{
    // Do your camera 'locking' here
}
Comment
Add comment · Show 2 · 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 · Apr 19, 2011 at 10:53 AM 0
Share

To clarify, your game is almost certainly 'crashing' when you destroy the player because it's trying to look at an object that no longer exists. By adding this ifcheck, you avoid the crash when the player has been destroyed, because the code won't be executed.

avatar image Max 4 · Apr 19, 2011 at 10:53 AM 0
Share

O$$anonymous$$, so there is no camera code, it's simply dragged into position and placed in the players heirachy.

avatar image
0

Answer by poncho · Apr 19, 2011 at 01:14 PM

when using the destroy method make sure to dont use the camera

if(Objecttodestroy != GameObject.Find(Camera))
{
GameObject.Destroy(Objecttodestroy);
}

or in the way you are destroying your objects

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroy object question! 3 Answers

Changing camera after being destroyed 1 Answer

How can I make the Player be destroyed when I touch the end of the camera? 1 Answer

How to destroy a projectile when it leaves any side of the camera view on a 2D game? 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