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 Courtneyssg · Oct 01, 2018 at 03:35 AM · scripting probleminstantiateprefabcollider2dtransform.translate

Script Not Working On Prefab

I seen some similar questions posted about this, but none of the ones I read were answered. I wrote a script that simulates a wind zone.

There is a collider on the wind zone game object that turns a bool "inwindzone" true when the player is colliding with it, false if not. When I have a pre-existing game object with the tag "Player" already in the hierarchy and pass the wind zone script the game object from the hierarchy , on game start the script works fine on the player. (Moves the player up for a period of time, pauses movement all together, moves the player down, then pauses) this repeats as long as the player is in the wind zone.

However when I delete the player from the hierarchy, add the player prefab to the wind zone script where it accepts the gameobject player, and have my spawn manager instantiate the player prefab in the scene on start, inside the wind zone, there is no movement. The wind zone does not effect the player prefab.

I thought collision detection might be an issue, but even if I move the instantiated player outside of the wind zone and move it back in the inwindzone bool is true, however nothing else is performed on it, or so it seems. The tag is still labelled player. I'm not sure why this isn't working, I'm still relatively new to unity and still learning!

My player script just contains the player movement so I didn't bother to post that, but I have attached my wind zone script and spawn manager script that instantiates the player below:

 public class WindArea : MonoBehaviour {
     
         public GameObject player;
         public float strength;
         public bool inWindZone = false;
         public bool movingUp = true;
         public bool movingDown = false;
         public bool pauseUp = false;
         public bool pauseDown = false;
     
         void OnTriggerEnter2D(Collider2D other)
         {
             if (other.tag == "Player")
             {
                 inWindZone = true;
             }
         }
 
         void OnTriggerExit2D(Collider2D other)
         {
             if (other.gameObject.tag == "Player")
             {
                 inWindZone = false;
             }
         }
     
         void Update()
         {
             {
                 if (pauseUp)
                 {
                     StartCoroutine(DelayUp());
                 }
     
                 else if (pauseDown)
                 {
                     StartCoroutine(DelayDown());
                 }
     
                 else if (movingUp)
                 {
                     StartCoroutine(Moveup());
                 }
     
                 else if(movingDown)
                 {
                     StartCoroutine(Movedown());
                     
                 }
             }
         }
 
         IEnumerator Moveup()
         {
             if(inWindZone)
             {
                 player.transform.Translate(new Vector2(0, 1) * strength * Time.deltaTime);
             }
             yield return new WaitForSeconds(3);
             movingUp = false;
             movingDown = false;
             pauseDown = false;
             pauseUp = true;
         }
     
         IEnumerator DelayUp()
         {
             yield return new WaitForSeconds(5);
             movingUp = false;
             movingDown = true;
             pauseUp = false;
             pauseDown = false;
         }
 
         IEnumerator Movedown()
         {
             if (inWindZone)
             {
                 player.transform.Translate(new Vector2(0, -1) * strength * Time.deltaTime);
             }
             yield return new WaitForSeconds(3);
             movingDown = false;
             movingUp = false;
             pauseUp = false;
             pauseDown = true;
         }
 
         IEnumerator DelayDown()
         {
             yield return new WaitForSeconds(5);
             movingDown = false;
             movingUp = true;
             pauseUp = false;
             pauseDown = false;
         }
     }
 
 
 
 
 public class SpawnManager : MonoBehaviour {
 
     [SerializeField]
     private GameObject _player;
 
     // Use this for initialization
     void Start () {
         Instantiate(_player, new Vector2(0, -27), Quaternion.identity);
     }
     
     // Update is called once per frame
     void Update () {
 
         if (_player.transform.position.y <= -35)
         {
             StartCoroutine(Respawn());
         }
     }
 
     IEnumerator Respawn()
     {
         Destroy(this.gameObject);
         yield return new WaitForSeconds(3);
         Instantiate(_player, new Vector2(0, -30), Quaternion.identity);
     }
 }



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 Courtneyssg · Oct 01, 2018 at 02:58 PM 0
Share

I also forgot to mention I'm not getting any errors or warnings on runtime.

2 Replies

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

Answer by LCStark · Oct 01, 2018 at 03:11 PM

Your WindArea script is changing the position of the prefab, not the instanced object. If you change the transform of the prefab, it'll have no effect on already created instances. In your case, it won't have any effect at all - when you instantiate your object, you position it at Vector2(0, -27), so you ignore the prefab position.

In your SpawnManager class you create an instance of the player object, but you don't store your reference anywhere. You have to either store a reference to your new instance in a field of that class and pass it to the wind area script, or use something like GameObject.Find to find your object in the scene from the wind area script directly.

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

Answer by Courtneyssg · Oct 01, 2018 at 05:24 PM

Thanks, this was the issue. I didn't realize I had to create a reference to the instance. I created a public variable public Player player and after my prefab gets instantiated I used FindObjectOfType<Player>(). If player gets destroyed I instantiate it again and use FindObjectOfType<Player>() in the Respawn Coroutine.

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 LCStark · Oct 01, 2018 at 05:29 PM 0
Share

Yeah, that should work fine. If your public Player player is in the Spawn$$anonymous$$anager class, you don't even need to use FindObjectOfType - you can simply use the return value of Instantiate:

IEnumerator Respawn()
{
    Destroy(this.gameObject);
    yield return new WaitForSeconds(3);
    GameObject playerGO = Instantiate(_player, new Vector2(0, -30), Quaternion.identity);
    player = playerGO.GetComponent<Player>();
}

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

192 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

Related Questions

How to create GameObject (from prefab) at location of BoxCollider2D with rotation? 0 Answers

Instantiate Objects from text file 1 Answer

BoxCollider2D failling verification when Instantiate (Fixed, Cause: big derp) 1 Answer

Instantiate a prefab and then add a animator component and controller to the component. 2 Answers

Put a prefab in Hierarchy without using Instantiate ? (from code) 2 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