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 /
This question was closed Sep 22, 2015 at 04:39 AM by cwnichols for the following reason:

Solved.

avatar image
0
Question by cwnichols · Sep 21, 2015 at 07:49 PM · prefabtouchvariablechanging

Variable changes back to default after Start()

Hello,

I am working on a 2D mobile game where the player object movement is restricted to left/right in the x-axis. Movement is also set to line up with specific points along the x-axis that correspond with columns of enemies, that way when the player object fires it will always hit an enemy. I have touch pads along the x-axis of the player object that line up with those columns as well, the pads have a touch script that should make the player object move to that pad and fire when the player touches that pad. The problem is that sometime between Start() and when the player touches the pad, the variable that holds the information about which column that pad lines up with resets to 0. These are prefabs by the way, instantiated during the start of the level in a separate game controller script and the variable is set by a function call right after the pad object is instantiated.

The GameController code that instantiates the touch pad and sets it's column variable to i:

 //the firePoints[i] array holds vector3 spawn points for the player movement touch pads
 for(int i = 0; i < columns; i++){
             GameObject temp = Instantiate(firePoint, firePoints[i], Quaternion.identity) as GameObject;
             Debug.Log("i = " + i);
             temp.GetComponent<PlayerTouchPad>().SetColumn(i);
         }


The PlayerTouchPad class:

 public class PlayerTouchPad : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
 
     int fireColumn;
     bool isTouched;
     int touchID;
     GameObject player;
 
     void Awake(){
 
         isTouched = false;
 
         Debug.Log("Awake firecolumn = " + fireColumn);
     }
 
     void Start(){
 
         player = GameObject.FindGameObjectWithTag ("Player");
         Debug.Log("Start firecolumn = " + fireColumn);
     }
 
     public void OnPointerDown(PointerEventData data) {
 
         if (!isTouched) {
 
             isTouched = true;
             touchID = data.pointerId;
         }
     }
 
     public void OnPointerUp(PointerEventData data) {
 
         if (data.pointerId == touchID) {
 
             player.GetComponent<PlayerController>().Mover(fireColumn);
         }
         isTouched = false;
     }
 
     public void SetColumn(int column){
 
         fireColumn = column;
     }


The PlayerController Mover() method:

 public void Mover (int column)
     {
         int move = 0;
 
         Debug.Log ("Column = " + column);
 
         if (!inFlight && !isMoving) {
 
             Vector3 direction = controller.firePoints[column];
 
             isMoving = true;
             move = (int)direction.x;

             //Starts a coroutine to smoothly move the player to the point 'direction'
             StartCoroutine (SmoothMove (direction, move));
         }
     }

The debug line in PlayerTouchPad.Start() outputs the correct column numbers(0, 1, 2, 3, 4 in the case of 5 columns, which is as expected). However when the PlayerController.Mover(int) script is called in the OnPointerUp method, the debug output in the Mover method returns 0, no matter which pad I touch.

I am really not sure why that variable keeps resetting, I did some searching and could not find an answer, but my search-fu may have been weak.

Any suggestions are welcome, thank you.

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

  • Sort: 
avatar image
0

Answer by cwnichols · Sep 22, 2015 at 07:11 AM

Turned out I had somehow attached the script to a UI object without realizing it, it was invisible during game play, and it was the thing intercepting touches. Closing this now.

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

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Is it possible to show Static Variables in the Inspector? 10 Answers

prefabs not loading script reference via getComponent 1 Answer

How do I assign a prefab to a variable with a script? 1 Answer

Quickest method to change a variable on another gameobject. 2 Answers

Change prefab color when shuriken particle touches it 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