• 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
Question by laurienash · Jun 04, 2013 at 01:38 PM · instantiatethirdpersoncontroller

Instantiate new third person controller at the exact point first third person controller is destroyed

Hallo, In my game my third person controller is destroyed when he hits a box collider, and a new one instantiated in his place.

At the moment the prefab is instantiated in the middle of the box collider, but I was wondering if there is a way of instantiating the new third person controller at the exact point the first third person controller is destroyed?

This is the script I'm using to instantiate my new third person controller (JavaScript):

 #pragma strict
 
 var prefab : GameObject;
 var script : SwitchCharacters;
 var vincentCamera : GameObject;
 
 var playerGO : GameObject;  //Should be filled with the GameObject the SwitchCharacters script is attached to
 
 
 private var hasPlayed = false;
 
 
 function OnTriggerEnter () {
 
 var pos : Vector3; 
 
 if (!hasPlayed){
 
 var newprefab = Instantiate (prefab);
 var cameraofnewPrefab = Instantiate(vincentCamera);
 
 
 script = playerGO.GetComponent(SwitchCharacters);
 script.player02 = newprefab;
 script.cam02 = cameraofnewPrefab; 
 
 hasPlayed = true;
 
 }
 
 }

I found a lot on the forums about adding Instantiate(prefab, Vector3(0,0,0),Quaternion.identity); but I can't work out how to instantiate the character at the exact point where the first character was destroyed.

Any ideas?

Thanks, Laurien

Comment
cregox

People who like this

1 Show 0
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

Answer by SubatomicHero · Jun 04, 2013 at 01:48 PM

You need to get the position of the first character before you destroy it by storing it in a varaible. Then when you instantiate your new prefab, you pass in that Vector:

 Instantiate(prefab, firstCharacterPosition, Quaternion.identity);
Comment
laurienash
cregox

People who like this

2 Show 8 · 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 laurienash · Jun 04, 2013 at 01:53 PM 0
Share

Sorry (very new to gaming but hopefully I'll get there) but how do I store the position of the first character in a variable? And won't that be destroyed once the first character is destroyed? Do I have to use rigidbody.position = Vector3.zero?

avatar image SubatomicHero · Jun 04, 2013 at 01:58 PM 1
Share

No worries Laurien. Before you call the destroy function to destroy your first game object, you create a variable like so:

 var firstCharacterPosition : Vector3;
 
 // Then just before you call destroy
 firstCharacterPosition = firstCharacter.transform.position // firstCharacter is the name of the gameobject
 Destroy(firstCharacter);
avatar image moghes · Jun 04, 2013 at 03:06 PM 1
Share

@laurienvictoria you can set this answer as true :) that yould help others searchers.

avatar image Scribe · Jun 05, 2013 at 04:45 PM 1
Share

To get the variable from the first script you need to use GetComponent(), if the scripts are attached to the same object then you can use:

 var prefab : GameObject;
 var characterPosition : Vector3;
 private var hasPlayed = false;
 
 function OnTriggerEnter () {
     if (!hasPlayed){
         characterPosition = GetComponent("ScriptName").firstCharacterPosition;
         Instantiate (prefab, characterPosition, Quaternion.identity);
     hasPlayed = true;
     }
 }

where "ScriptName" should be replaced by the name of the first script (where you save the variable)

If it was attached to a different gameobject you have to use GameObject.GetComponent where you will need either a way to find the gameobject, or have that saved as an additional variable.

If the first script was attached to an object that was then deleted I think you will need to look at Static variables (not certain of that!)

Scribe

avatar image Scribe · Jun 06, 2013 at 04:16 PM 1
Share

It's is possible that this is simply because the second script trigger event is being called just prior to the DestroyCharacter script. To your InstantiateNewCharacter script try adding var thirdPerson : GameObject; and change if (!hasPlayed) to if (!hasPlayed && !thirdPerson.active)

Show more comments

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

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Script that switches between first and third person controller 3 Answers

Instantiated Particles reverse direction. Why? 1 Answer

Intantiate problems 1 Answer

Instantiate a prefab through code in C#. 4 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