0
\$\begingroup\$

I am trying to make a respawn system where if the player touches a plane, (the name of the object is tutlvl1_flrdead) they get teleported to an empty object at player spawn named "RESPAWN". But, several tutorials and unity answers threads lead me to nowhere since they were written from back in 2013 or somewhere around there.

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class deathfloor : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
    
    }
    
    // Update is called once per frame
    void Update()
    {
        public gameObject myObject; //You can name it whatever you want. 
        myObject.transform.position = new Vector3(3.076, 1.74, -3.651);
    } 
}

How can I fix this to do what I need for my game?

\$\endgroup\$
3
\$\begingroup\$

You've added only two lines that are not in the default script template, and neither of them are correct. One of them is illegal and will not compile, the other is simply useless and does nothing related to what you're aiming to achieve.

If you've really studied multiple tutorials and past Q&A threads and worked at this for days, I have to say that research and effort is not in evidence here, and you should very seriously consider putting this project on pause. Go pick up a beginner's tutorial - even if it's for a different kind of game than you want to make, and follow all the steps from beginning to end. Then do it again with a different tutorial. Then again with another.

Once you've gotten this amount of practice, you'll be able to write at least two lines of original code that actually compile, and you can consider resuming your current project with your better-developed understanding of how the language and engine work.

A script that does what you describe might look something like this:

// Class names use PascalCase, with an initial capital letter.
public class DeathFloor : MonoBehaviour {

    // Call this method when something enters the death floor's trigger volume.
    // Add a "2D" to the method name and Collider type if your game is 2D.
    // Be sure your player has a collider and dynamic rigidbody of character
    // controller component attached, and your floor's collider has its
    // "Is Trigger" box ticked.
    void OnTriggerEnter(Collider collider) {

        // Better yet, make this a member variable assigned in the inspector,
        // so you don't need to search for it at runtime.
        var spawnPoint = GameObject.Find("RESPAWN");

        if (spawnPoint == null) {
            Debug.LogError("Could not find a spawn point object named 'RESPAWN'");
            return;
        }

        // You might want to check whether collider is the player,
        // or some other object to ignore. Here I'll assume only the player
        // can fall into and activate this particular trigger.
        collider.gameObject.transform.position = spawnPoint.transform.position;
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Thank you so much, you are a life saver! \$\endgroup\$ – Zaxify Apr 6 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.