Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
This question was closed Apr 06, 2021 at 08:00 PM by Matt27832 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Matt27832 · Apr 06, 2021 at 04:48 AM · transformplayerspawningrespawn

Why is the Player Transform only changing correctly the first time?

So I am trying to make a 3D platformer for practice and I am confused on why the player spawn works on awake but then when it is triggered for respawning it doesn't work. Here is the Spawner Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Spawner : MonoBehaviour
 {
     Vector3 playerSpawn;
 
     [SerializeField] Transform Player;
 
     private void Awake()
     {
         Spawn(Player); //calls spawn
         Debug.Log("Spawning Finished");
         
     }
 
     public void Spawn(Transform p)
     {
         playerSpawn = GameObject.FindGameObjectWithTag("Respawn").transform.position; //sets playerSpawn to Spawner position
 
         Debug.Log(playerSpawn);
 
         p.transform.position = playerSpawn; //Sets Player transform to playerSpawn
 
         Debug.Log(p.transform.position);
 
         Debug.Log("Spawn Triggered");
     }
 }

And here is my script for respawning:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class KillBox : MonoBehaviour
 {
     public GameObject spawnerObject;
     public Spawner spawnerScript;
 
     [SerializeField] Transform Player;
     Vector3 playerSpawn;
 
     // Start is called before the first frame update
     void Start()
     {
         spawnerScript = spawnerObject.GetComponent<Spawner>();
     }
 
     public void OnTriggerEnter(Collider other)
     {
         spawnerScript.Spawn(Player);
     }
 }






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 KoenigX3 · Apr 06, 2021 at 08:38 AM 0
Share

Are you sure that OnTriggerEnter gets called? Test it with Debug.Log. What about the Player transform, are they pointing to the same transform?

Also, if you don't need playerSpawn globally, you could use it locally, like this:

 Vector3 playerSpawn = GameObject.FindGameObjectWithTag("Respawn").transform.position;


Keep in $$anonymous$$d that searching for gameobjects on runtime can decrease your performance if you have a lot of gameobjects in your scene. Either store the position in your code or get it through a reference.

avatar image Matt27832 KoenigX3 · Apr 06, 2021 at 07:35 PM 0
Share

Yes, OnTriggerEnter does get called, and looks like it runs through the spawning script in the stack and returns back out. I manually placed the player transform in and they both point to the same player object. It looks like it goes through the script but doesn't change the XYZ transform for some reason. And no warnings or errors pass through.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Matt27832 · Apr 06, 2021 at 07:59 PM

Figured it out. So it turns out I just had to disable the player controller before calling the spawn method or I assume it would overwrite the transform change since the gravity is also changing the transform. And then just re-enable the controller after the method is called.

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

Follow this Question

Answers Answers and Comments

213 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 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

NetworkTransform did not working with RegisterSpawnHandlers 2 Answers

Player respawn after collision 0 Answers

Diagonal character movement 0 Answers

How to Increase Value as Player Approaches 0 Answers

How to automatically assign a Transform? 2 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