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 Chedora · Jan 29, 2014 at 03:20 AM · javascriptinstantiateprefab

After instantiating an instance of a prefab and changing the value of a variable in a script attached to the prefab, that value is lost

I'm making a simple sidescrolling 2D game where the player is either facing right or left (based on whether they have moved left or moved right most recently). The player should be able to shoot a missile in front of them and have it travel in the direction they are facing. I do this using this code in the Update function of the script controlling the player:

 //Shoot
     if((Time.time - lastShot) >= shotWait) {
         canShoot = true;
     }
     if(Input.GetKey(KeyCode.E) && canShoot) {
 
         var control: PlayerShotControl;
         if(direction == "Right") {
             Debug.Log("Shooting, direction is right");
             Instantiate(shot, transform.position + Vector3(0.75f, 0.0f, 0.0f), Quaternion.Euler(0.0f, 0.0f, 270.0f));
             control = shot.GetComponent(PlayerShotControl);
             control.direction = "Right"; 
             Debug.Log("After assignment, control.direction is " + control.direction);
         } else if (direction == "Left") {
             Debug.Log("Shooting, direction is left");
             Instantiate(shot, transform.position + Vector3(-0.75f, 0.0f, 0.0f), Quaternion.Euler(0.0f, 0.0f, 270.0f));
             control = shot.GetComponent(PlayerShotControl);
             control.direction = "Left"; 
         Debug.Log("After assignment, control.direction is " + control.direction);
         } else Debug.Log("Spaceship direction = " + direction);
             canShoot = false;
         lastShot = Time.time;

"shot" is the prefab for the missile I want to shoot. The Update function for the script controlling the shot is this:

 function Update () {
     if(direction == "Right") {
         Debug.Log("Shot moving. Direction is right");
         transform.position.x += moveSpeed;
     } else if (direction == "Left") {
         Debug.Log("Shot moving. Direction is left");
         transform.position.x -= moveSpeed;
     } else Debug.Log("Shot Direction is " + direction);

When I actually have the player shoot, it works fine except for the first shot after I have changed direction. The character is originally acing right. If I shoot without changing direction, the missile fires to the right like it should. Once I turn left and shoot again, the shot is instantiated at the correct location for shooting left(Notice in the player's Update function the shot's x value is different depending on whether it is shooting right or left), however, it goes right. If I fire again while still facing left, it works like it should. It continues to work correctly except that the first shot after changing directions is fired in the wrong direction.

The Debug statements my code show that the player's direction is always correct, and it always sends the correct direction to the shot. So after turning left, the debug shows "Shooting, direction is left" and "After assignment, control.direction is Left". However, the debug in the shot's Update says that its value for direction is "Right" even though it was just set to "Left" and never changed. So after turning left, shooting, and getting the debug messages from the player above, the Shot still reports "Shot moving. Direction is right".

Can anyone offer some insight as to what is going on here? Any help would be greatly appreciated. Also if I missed some important part of my script that should be here, let me know and I will post it. Thanks!

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 Ashish Dwivedi · Jan 29, 2014 at 04:23 AM

Recently I also faced the problem by mistake. I was accessing the prefab script not that instance script.

GameObject obj = (GameObject) Instantiate(shot, transform.position + Vector3(0.75f, 0.0f, 0.0f), Quaternion.Euler(0.0f, 0.0f, 270.0f));

control = obj.GetComponent(PlayerShotControl);

control.direction = "Right";

In the second line of your code which i pasted, reference of that instance (means "obj") must be there instead of "shot". I think this would help you.

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 Chedora · Jan 29, 2014 at 05:36 AM 0
Share

Thank you! That did it!

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

19 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Problems instantiating a prefab (javascript) 1 Answer

Instantiate a Prefab thats been passed through 2 scripts? 2 Answers

How to set a default prefab on a C# script? 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