Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 stewie_7999 · Apr 25, 2019 at 10:54 AM · scripting problemprefab

Prefab loses script transform values.

I'm making a platformer and I have objects which when they collide with the player, the player is spawned back at a respawn point. My respawning script on each set of spikes has player and respawn point transform values. However when I make the spikes into a prefab in order to place several of these spikes around the level, the prefab's player and respawn point transform values are lost. Only the originally placed spikes have the values. I know i could just manually add these transforms to each instance of the prefab (each set of spikes in the level) but this would be tedious if I'm going to make many levels.

Sorry for the the noob, this is my first game.

The script for respawn is shown below:

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

public class Respawn : MonoBehaviour { [SerializeField] private Transform player; [SerializeField] private Transform respawnPoint;

 private void OnTriggerEnter2D(Collider2D other)
 {
     player.transform.position = respawnPoint.transform.position;
 }

}

originally-placed-spikes.png (107.8 kB)
prefabspikes.png (165.5 kB)
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 KevRev · Apr 25, 2019 at 02:53 PM 0
Share

It's much easier to just give the spikes a tag such as "Spike".

Have an empty gameobject called Respawn point.

Have a script on your player prefab along the lines of:

 public void OnCollission(Collider col)
 {
   if (col.CompareTag("Spike"))
     {
       transform.position=Respawn.transform.position;
       lives-=1;
       Instantiate awesome magical particle effect;
      }
    }
 }

2 Replies

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

Answer by abi-kr01 · Apr 25, 2019 at 11:02 AM

Prefab only have links that are either part of prefab itself or something from project folder like images. When you are generating this prefab in the scene you have to assign the player in

 using UnityEngine;
 using UnityEngine.UI;
 public class Respawn: MonoBehaviour
 {
   public Transform player;
   public Transform respawnPoint;
  void Start()
     {
      player = GameObject.Find("Player");
      RespawnPoint=GameObject.Find("RespawnPoint");
      }
 }



Hope you have only one player and RespawnPoint

Comment
Add comment · Show 1 · 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 stewie_7999 · Apr 26, 2019 at 11:10 AM 0
Share

I tried all methods suggested to me and this is the only one which worked in my case. Thanks!

avatar image
2

Answer by Bunny83 · Apr 25, 2019 at 11:05 AM

Of course. Prefabs are assets. Assets can never reference objects that live in a scene. The objects in a scene are only available when the scene is loaded. If you instantiate the prefab you have to setup any scene object references you need yourself from the instantiating script or let a script on the prefab find the references itself. The first option is the better one. Though common things like respawning shouldn't be handled by individual objects / obstacles. It's common to create some sort of game manager singleton which provides a Respawn method. Any other object can simply call that method on the singleton.

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

202 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

Related Questions

Access object script from prefab script 1 Answer

scripting and prefab saving error 0 Answers

How to obtain this prefab name in this circunstances 1 Answer

How can I apply a text variable to a prefab 2 Answers

How do I get an object to Instantiate infront of a specific object/player? 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