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 AevinGames · Jun 13, 2016 at 04:47 PM · c#2dinstantiate

HELP, Scripts dont work after instantiation.

Hey. Im going to cut this short. The game is a 1v1 melon bomber. When the objects are killed, they are destroyed. Then a respawn script is triggered and the gameobjects are spawned but they wont move. HELP, no forum post helped.

SpawnManager:

 public class SpawnManager : MonoBehaviour {
 
     public GameObject PlayerOne;
     public GameObject PlayerTwo;
 
     public static GameObject Player1Prefab;
     public static GameObject Player2Prefab;
 
     public Transform PlayerOneSpawn;
     public Transform PlayerTwoSpawn;
 
     void Start () {
         Spawn ();
     }
 
     void Update () {
         if (ScoreManager.ReSpawn == 1) {
             Spawn ();
             ScoreManager.ReSpawn = 0;
         }
     }
     
     // Update is called once per frame
     public void Spawn () {
         Player1Prefab =(GameObject) Instantiate(PlayerOne, PlayerOneSpawn.transform.position, PlayerOneSpawn.transform.rotation);
         Player2Prefab =(GameObject) Instantiate(PlayerTwo, PlayerTwoSpawn.transform.position, PlayerTwoSpawn.transform.rotation);
     }
 }

PlayerHealth (Second player is the exact same):

 public class PlayerHealth : MonoBehaviour {
 
     public static int currentHealth;
 
     void Awake () {
         currentHealth = 100;
     }
 
     void Update () {
         Debug.Log (currentHealth);
 
         if (currentHealth <= 0){
             Debug.Log ("SCOOORE");
             ScoreManager.PlayerTwoScore = ScoreManager.PlayerTwoScore + 1;
             ScoreManager.NextRound = true;
             DestroyThis();
         }
 
         if (ScoreManager.NextRound == true) {
             DestroyThis();
         }
     }
     
     void DestroyThis () {
         Destroy(this.gameObject);
     }
 }
 
 

Movement Script:

 public class PlayerOneController : MonoBehaviour {
     
         public float speed = 1.5f;
     public GameObject PlayerOneBomb;
     public GameObject PlayerOneDamageR;
         public static int BombsOne;
 
         void OnEnable () {
         BombsOne = 1;
         }
 
         void Update ()
         {
             if (Input.GetKey(KeyCode.A))
             {
                 transform.position += Vector3.left * speed * Time.deltaTime;
             }
             if (Input.GetKey(KeyCode.D))
             {
                 transform.position += Vector3.right * speed * Time.deltaTime;
             }
             if (Input.GetKey(KeyCode.W))
             {
                 transform.position += Vector3.up * speed * Time.deltaTime;
             }
             if (Input.GetKey(KeyCode.S))
             {
                 transform.position += Vector3.down * speed * Time.deltaTime;
             }
 
             if (Input.GetKey (KeyCode.Space)) {
             if(BombsOne >= 1){
                 BombsOne= BombsOne - 1;
                 Instantiate(PlayerOneBomb, this.transform.position, this.transform.rotation);
                 Instantiate(PlayerOneDamageR, this.transform.position, this.transform.rotation);
                 StartCoroutine(Resupply());
             }
         }
     }
 
     IEnumerator Resupply (){
         yield return new WaitForSeconds (2);
         BombsOne = BombsOne + 1;
     }
 }
 
 


Thanks

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 Dibbie · Jun 14, 2016 at 07:31 AM 1
Share

With your prefab, are these 2 scripts (your movement and health) attached to it? When you have a prefab and you have variables you want to essentially "drag and drop" into the inspector, it will only look at Asset objects, so essentially other files or prefabs, not objects in the active scene as the Inspector normally would with game objects. So you would either need to default these public variables so that the script can still work properly once re-spawned, or re-set these variables.

Also make sure that both scripts are actually enabled on your prefab.

avatar image wojtask12 · Jun 14, 2016 at 09:29 AM 1
Share

Your "Spawn" mothod seems to be a problem.

 Player1Prefab =(GameObject) Instantiate(PlayerOne, PlayerOneSpawn.transform.position, PlayerOneSpawn.transform.rotation); 

   

It should probably look more like

   PlayerOne =(GameObject) Instantiate(Player1Prefab, PlayerOneSpawn.transform.position, PlayerOneSpawn.transform.rotation);

Instantiating is creating an scene object of prefab, so the prefab object should be a "source" for this operation- that's why you should pass it as parameter and return the result of instantiating (which is spawned scene object) to your PlayerOne field

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Object instantiates on top of the previous object 2 Answers

Question about Instantiate 1 Answer

How to generate different diagonal platforms? 0 Answers

Unity2D Instantiated Prefab Won't Appear in Scene 1 Answer

2D Projectile Not Firing Based on Rotation 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