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 /
  • Help Room /
avatar image
0
Question by gayzare · Dec 08, 2020 at 07:38 PM · instantiateprefabinspectorprogramming

Player prefab being unassigned when enemy is instantiated

The issue I am having is when I instantiate my enemy into the scene, the enemy is meant to start following the player, however when they spawn, the player game object, becomes unassigned in the inspector.

When I bring the prefab back into the scene and assign it myself the players transform position myself without deleting the enemy it works fine, and the enemy starts to follow the player, but basically as soon as its instantiated, the players transform is unassigned in the inspector.

Thank you if your able to help!

public class SpawnManager : MonoBehaviour { public GameObject enemyPrefab; public GameObject playerPrefab; private float startDelay = 2; private float repeatRate = 2; // Start is called before the first frame update void Start() { InvokeRepeating("SpawnEnemy", startDelay, repeatRate); } // Update is called once per frame void Update() { } void SpawnEnemy() { Instantiate(enemyPrefab, new Vector3(0f, 0f, 50f), enemyPrefab.transform.rotation); } } public class EnemyScript : MonoBehaviour { // Wheel Collider Fields [SerializeField] WheelCollider FLWC; [SerializeField] WheelCollider FRWC; [SerializeField] WheelCollider BLWC; [SerializeField] WheelCollider BRWC; // Wheel Transform fields [SerializeField] Transform FLW; [SerializeField] Transform FRW; [SerializeField] Transform BLW; [SerializeField] Transform BRW; // Public Variables public float motorTorque = 100f; public float maxSteering = 20f; public Transform centerOfMass; public Transform player; public float speed = 4f; // Private Variables private Rigidbody carRb; private Rigidbody enemyRb; // Start is called before the first frame update void Start() { enemyRb = GetComponent<Rigidbody>(); carRb = GetComponent<Rigidbody>(); carRb.centerOfMass = centerOfMass.localPosition; } // Update is called once per frame void FixedUpdate() { Vector3 pos = Vector3.MoveTowards(transform.position, player.position, speed * Time.deltaTime); enemyRb.MovePosition(pos); transform.LookAt(player); WheelRotation(); } private void WheelRotation() { var pos = Vector3.zero; var rot = Quaternion.identity; FLWC.GetWorldPose(out pos, out rot); FLW.position = pos; FLW.rotation = rot; FRWC.GetWorldPose(out pos, out rot); FRW.position = pos; FRW.rotation = rot * Quaternion.Euler(0, 180, 0); BLWC.GetWorldPose(out pos, out rot); BLW.position = pos; BLW.rotation = rot; BRWC.GetWorldPose(out pos, out rot); BRW.position = pos; BRW.rotation = rot * Quaternion.Euler(0, 180, 0); } }

Comment
Add comment
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
0
Best Answer

Answer by highpockets · Dec 08, 2020 at 07:46 PM

The enemy script which is on the enemy’s you spawn has the player prefab assigned to the player property of it, not the instance of the player in the scene. You cannot assign an instance of a prefab to a property of a class of a prefab that is not yet in the scene. You have to make that connection when they spawn because a reference to a prefab is just a reference to a game object which is not an instance.


 void Start()
 {
     player = GameObject.FindWithTag(“PlayerTagName”);
 }

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 gayzare · Dec 11, 2020 at 10:56 AM 0
Share

Thank you for the help!

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

296 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Animations created in Runtime 0 Answers

I get an error message every time i run this script? Any ideas? 0 Answers

Unable to set position of instantiated prefab 1 Answer

I need help destroying a clone on function. 1 Answer

Instantiated object retains variable values 0 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