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 /
avatar image
0
Question by Cyrax657 · Jan 16, 2015 at 04:31 AM · c#scripting problem

Instantiate not behaving as expected with enabling/disabling Collider

Attempting to add a invincibility period script, when the player is Destroyed a respawn sequence begins. Respawn works fine however the collider remains disabled and doesn't reenabled after it Instantiates the player. Only time enabling the collider works if the enable code is before the Instantiate code, but that doesn't allow for brief invincibility after respawn.

 using UnityEngine;
 using System.Collections;
 
 public class Spawn : MonoBehaviour {
 
     public GameObject Player;
     public static bool alive;
     Collider Safety;
 
     void Start ()
     {
         Vector3 respawnPoint = new Vector3 (0, 0, 0); //Where the player intially spawns
         Instantiate (Player, respawnPoint, Quaternion.identity);
         alive = true; //Player Status is Alive
     }
 
     void Update ()
     {
         if (alive == false) //If Player is Destroyed(Status Changed to False by DestroyByContact Script)
         {
             StartCoroutine(respawn()); // Begin Respawn Sequence
             alive = true; //Reset layer to Alive Status 
         }
     }    
 
     IEnumerator respawn()
     {
         RespawnCounter.respawning = true; //Enables respawning Text UI in RespawnCounter Script
 
         yield return new WaitForSeconds (3.0f); //Wait 3 seconds before respawning
         RespawnCounter.respawning = false;  //Disables Respawning Text UI
 
         Safety = Player.GetComponent<Collider>(); 
         Safety.enabled = false;        //Disables Collider for intial respawn Safety
         Debug.Log (Safety.enabled);
 
         Vector3 point = new Vector3 (0,0,0);        //Respawns player back at Starting point
         Instantiate(Player, point, Quaternion.identity);
 
         yield return new WaitForSeconds(1.5f); //Enables Collider after 1.5 seconds
         Safety.enabled = true;
         Debug.Log (Safety.enabled);
     }
 }



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 SanX91 · Jan 16, 2015 at 04:49 AM

public class Spawn : MonoBehaviour {

  public GameObject Player;
  public GameObject playerPrefab;
  public static bool alive;
  Collider Safety;
 
  void Start ()
  {
      Vector3 respawnPoint = new Vector3 (0, 0, 0); //Where the player intially spawns
      Player = Instantiate (playerPrefab, respawnPoint, Quaternion.identity) as GameObject;
      alive = true; //Player Status is Alive
  }
 
  void Update ()
  {
      if (alive == false) //If Player is Destroyed(Status Changed to False by DestroyByContact Script)
      {
          StartCoroutine(respawn()); // Begin Respawn Sequence
          alive = true; //Reset layer to Alive Status 
      }
  }    
 
  IEnumerator respawn()
  {
      RespawnCounter.respawning = true; //Enables respawning Text UI in RespawnCounter Script
 
      yield return new WaitForSeconds (3.0f); //Wait 3 seconds before respawning
      RespawnCounter.respawning = false;  //Disables Respawning Text UI
 
      Safety = Player.GetComponent<Collider>(); 
      Safety.enabled = false;        //Disables Collider for intial respawn Safety
      Debug.Log (Safety.enabled);
      Destroy(Player);
      Vector3 point = new Vector3 (0,0,0);        //Respawns player back at Starting point
      Player = Instantiate(playerPrefab, point, Quaternion.identity) as GameObject;
 
      yield return new WaitForSeconds(1.5f); //Enables Collider after 1.5 seconds
      Safety = Player.GetComponent<Collider>(); 
      Safety.enabled = true;
      Debug.Log (Safety.enabled);
  }

}

Try this and check.. Make a player prefab first instead.. Just a quick review... Things can be further improved if needed.

Comment
Add comment · Show 2 · 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 Cyrax657 · Jan 16, 2015 at 03:11 PM 0
Share

$$anonymous$$oved lines 29 and 30 to after Instantiating the playerPrefab and Removed the Destroy(Player) line since another script does that and it worked. Thanks a bunch! Do you know why when I tried doing " Player = Instantiate(playerPrefab, point, Quaternion.identity) as Collider; " it didn't work as expected?

$$anonymous$$y guess is that I need to store my playerPrefab info into Player first then access the Collider, can't jump straight to accessing the Collider like what I tried.

avatar image SanX91 · Jan 16, 2015 at 04:39 PM 0
Share

yup, u're right. when a prefab is instantiated u can't directly access it unless u pass it in a variable.. That's because u won't have any reference to it within the script.

This line instantiates your player:

Instantiate(Player, point, Quaternion.identity);

but u can't really do anything more with that instantiated object within that particular script, unless u do this:

player = Instantiate(playerPrefab, point, Quaternion.identity) as GameObject;

Now, the player is under your control

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how do i add a offset to my sword rotation? 2 Answers

How to make a GameObject a button? 2 Answers

audio doesn't play on movement 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