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 Romano185 · Feb 13, 2012 at 06:10 PM · instantiateplayervariablestoretransport

How to store a variable of the player

Hi everybody

I'm making a script to use for my cart system. This system exists out of a cart which moves on the same route thanks due a loop animation, and a trigger object attached to the car with this script:

 static var destroyedobject : GameObject;
 
 function Update(){
 if(ClassSelectionScript.classselected == true && CartScript.seated == false){
 destroyedobject = GameObject.FindWithTag("Player");
 }
 }
 
 function OnTriggerEnter(otherObj: Collider){
 if (otherObj.tag == "Player"){
 Destroy(destroyedobject);
 CartScript.seated = true;
 }
 }

The cart also has a script attached:

 static var stopcommand : boolean = false;
 static var seated : boolean = false;
 
 function Update () {
 
 if(seated == true){
 stopcommand = true;
 }
 }

And the place the player leaves the cart, just a ordinary mesh with a trigger object, which has this script attached:

 var instantiatedobject : GameObject;
 
 
 
 function Update (){
 
 if (CartTrigger.destroyedobject != null){
 
 instantiatedobject = CartTrigger.destroyedobject;
 
 }
 
 }
 
 
 
 function OnTriggerEnter(otherObj: Collider){
 
 if (CartScript.stopcommand == true){
 
 var instance : GameObject = Instantiate(instantiatedobject, transform.position+Vector3(1,0,0), transform.rotation);
 
 CartScript.stopcommand = false;
 
 CartScript.seated = false;
 
 CartTrigger.destroyedbject = null;
 
 }
 
 }

What I'm trying to do is making the cart store the player and all the scripts and variables attached to it after destroying him, and instantiating him at the stopping point. The script destroys the player, but it doesn't instantiate him when the cart enters the trigger. Does the script maybe reset the variable when the player is killed?

Please help me solve this problem!

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 shaneapp · Feb 14, 2012 at 01:25 AM 0
Share

Why do you need to destroy and recreate like this? What are you trying to do?

avatar image Romano185 · Feb 14, 2012 at 03:45 PM 0
Share

I'm trying to simulate the player entering the cart and stepping out off it when the cart reaches a stopping point. And I think this is the only way to do it, because when I instantiate the prefab of the player all its variables are reset.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Feb 14, 2012 at 05:07 PM

It would be easier to unactivate the player (destroyedobject.active = false in your case). By the way, you should not use a Find method in update, it's to expensive. Or at least, test if the variable is null before searching.

If you really want to destroy/instantiate, you need a reference to the prefab from the assets folder, not the instantiated object, because you are destroying it. And you can't find this one with a Find function. Three (more two and a half really) ways, to affect it in the inspector manually, to access it from another object containing that prefab (which have been affected from the inspector so...) or with the resources folder and the function Resource.Load.

Comment
Add comment · Show 5 · 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 Romano185 · Feb 14, 2012 at 06:47 PM 0
Share

.active doesn't work, it just freezes the player. I want the player to turn invisible and respawn at the stopping point. And I think one of the problems is triggering the Stoppingpoint Script. The animated cart doesn't seem to activate the trigger. How can I fix this?

avatar image Berenger · Feb 14, 2012 at 06:56 PM 0
Share

the property active control all the component. when false, all components are disabled (the opposite isn't true though). The object can't be visible if active is false, unless the mesh renderer and the meshfilter are on an other objects in the hierarchy. So you need the root, and then use this function.

avatar image Romano185 · Feb 15, 2012 at 06:14 PM 0
Share

Thanks very much, now the player vanishes when he enters the trigger. But the cart still doesn't activate the trigger attached to the stopping point. I think the collider of the cart doesn't work, because the cart is animated. How can I make the collider work again while keeping the animation?

avatar image Berenger · Feb 15, 2012 at 07:11 PM 0
Share

If the stopping point is at the last frame of the animation, you can use animation events.

Or, you can add a child with collider and rigidbody non-kinematic, without gravity and constrained on everything. That child will activate the trigger.

avatar image Romano185 · Feb 16, 2012 at 08:31 PM 0
Share

Thanks, it really works great now!

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

7 People are following this question.

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

Related Questions

Setting the variable on a GameObject to be instantiated (not yet existent) cs and js 2 Answers

Player prefab instantiation 1 Answer

Instantianig a teleporter prefabs with destination variables 1 Answer

I am trying to access a variable from another script. 1 Answer

How to prevent object (bullet) from instantiating twice ? (It instantiates once for tapping the screen, and once more when beginning to hold touch on screen) ? 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