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 /
  • Help Room /
avatar image
0
Question by heiseyashton · May 07, 2020 at 06:22 AM · errorintmethodvaluesmethods

Int returns 0 after value is assigned, ,Int returns 0 within certain method, Can't figure out why.

I have an int called "index" which is within a script "Body". I assign index in another script when I Instantiate the GameObject it's on. No where else is the value of index changed anywhere though whenever called through a certain method the value always returns 0. I even checked to make sure the value was assigned properly by checking the inspector and calling the value through update by Debug, but any time the value is called through the method it returns 0.

The first script is the "Player" Script which is where I created the new GameObject which has the "Body" script and where "index" is assigned. Index is assigned in the Method "AddBodyPart()"

 public GameObject head;
 public GameObject bodyPrefab;
 public GameObject waypointPrefab;
 GameObject lastWaypointPos;
 List<GameObject> waypoints = new List<GameObject>();
 public List<GameObject> bodyParts = new List<GameObject>();

 [SerializeField]int startAmount = 3;

 public float speed = 5;
 float xDir;
 float yDir;

 bool blockW = true, blockA, blockS, blockD = false; //Block the inversed direction so you dont go backwards

 void Start()
 {
     lastWaypointPos = null;
     blockW = true;
     //Starts the Snake moving up
     xDir = 0;
     yDir = 1;
     CreateBody();
 }

 void Update()
 {
     ForceMovement();

     if(MovementInput())
     { CreateWaypoint(transform.position); }
 }

 void ForceMovement()
 {
     //Forces Forward
     transform.position += new Vector3(xDir * Time.deltaTime * speed, yDir * Time.deltaTime * speed);
 }

 private bool MovementInput()
 {
     //Movement
     if (Input.GetKeyDown(KeyCode.W) && !blockW)
     { xDir = 0; yDir = 1; blockS = true; blockA = false; blockD = false; return true; }
     else if (Input.GetKeyDown(KeyCode.A) && !blockA)
     { xDir = -1; yDir = 0; blockD = true; blockW = false; blockS = false; return true; }
     else if (Input.GetKeyDown(KeyCode.S) && !blockS)
     { xDir = 0; yDir = -1; blockW = true; blockA = false; blockD = false; return true; }
     else if (Input.GetKeyDown(KeyCode.D) && !blockD)
     { xDir = 1; yDir = 0; blockA = true; blockW = false; blockS = false; return true; }
     else
     { return false; }
 }

 void CreateWaypoint(Vector2 positon)
 {
     GameObject waypoint = Instantiate(waypointPrefab, new Vector2(positon.x, positon.y), Quaternion.identity) as GameObject;
     lastWaypointPos = waypoint;
     AddNewWaypointToBodies(waypoint);
 }

 void CreateBody()
 {
     for (int i = 0; i < startAmount; i++)
     {
         AddBodyPart(i == 0);
         bodyParts[i].transform.position -= (Vector3.up * (i + 1)) / 2;

         if(i == 0) { bodyParts[0].GetComponent<Body>().targetFollow = head; } //Sets target path to Head
     }
 }

 //
 //  Assigned In the Method Below
 // VVVVVVVVVVVVVVVVVVVVVVVVV

 void AddBodyPart(bool firstBody)
 {
     GameObject body = Instantiate(bodyPrefab, transform.parent) as GameObject;
     bodyParts.Add(body);

     body.GetComponent<Body>().SetIndex(bodyParts.Count - 1);
     body.name = "body " + bodyParts.Count;

     if(!firstBody)
     {
         body.GetComponent<Body>().waypoints = bodyParts[bodyParts.Count - 2].GetComponent<Body>().waypoints;

         if (lastWaypointPos == null) {
             body.GetComponent<Body>().targetFollow = bodyParts[bodyParts.Count - 2];
         }
         else
         {
             body.GetComponent<Body>().AddWaypoint(lastWaypointPos);
         }
     }
     
 }

 void AddNewWaypointToBodies(GameObject waypoint)
 {
     for(int i = 0; i < bodyParts.Count; i++)
     {
         bodyParts[i].GetComponent<Body>().AddWaypoint(waypoint);
     }
 }

In the "Body" Script I never change its value though when I call it in the Method "SetNextPoint()" it returns 0, while in update and the inspector it returns the actual value it should be.

     public List<GameObject> waypoints = new List<GameObject>();
     public GameObject targetFollow;
     int index;
     float speed;
 
     public bool settingPoint = false;
 
     private void Start()
     {
         speed = FindObjectOfType<Player>().GetComponent<Player>().speed;
     }
 
     void Update()
     {
         Debug.Log(index + ": Update");
 
         if (!settingPoint)
         {
             if(targetFollow != null)
             {
                 if (waypoints.Count > 0 && targetFollow != waypoints[0])
                 { targetFollow = waypoints[0]; }
 
                 if (transform.position != targetFollow.transform.position)
                 { MoveToTarget(); }
                 else
                 {
                     SetNextPoint();
                 }
             }
             else
             { SetNextPoint(); }
         }  
     }
 
     private void MoveToTarget()
     {
         if (targetFollow != null)
         { transform.position = Vector2.MoveTowards(transform.position, targetFollow.transform.position, speed * Time.deltaTime); }
     }
 
     public void SetIndex(int index)
     { this.index = index; }
 
     public void AddWaypoint(GameObject newWaypoint)
     {
         waypoints.Add(newWaypoint);
     }

    //
    // RETURNS 0 IN THE METHOD BELOW
    // VVVVVVVVVVVVVVVVVVVVVVVVVVVV

     public void SetNextPoint()
     {
         settingPoint = true;
         waypoints.Remove(waypoints[0]);

         Debug.LogError(index);
 
         if (index != 0 && waypoints.Count > 0)
         { targetFollow = waypoints[0].gameObject; }
         else
         { SetTargetToPreviousBody(); }
         settingPoint = false;
     }
 
     private void SetTargetToPreviousBody()
     {
 
         Debug.LogError(index);
 
         if (index != 0)
         { targetFollow = GetComponent<Player>().bodyParts[index - 1]; }
         else { targetFollow = FindObjectOfType<Player>().gameObject;  }       
     }

,I have an int called "index" which is within a script "Body". I assign index in another script when I Instantiate the GameObject it's on. No where else is the value of index changed anywhere though whenever called through a certain method the value always returns 0. I even checked to make sure the value was assigned properly by checking the inspector and calling the value through update by Debug, but any time the value is called through the method it returns 0.

The first script is the "Player" Script which is where I created the new GameObject which has the "Body" script and where "index" is assigned. Index is assigned in the Method "AddBodyPart()"

 public GameObject head;
 public GameObject bodyPrefab;
 public GameObject waypointPrefab;
 GameObject lastWaypointPos;
 List<GameObject> waypoints = new List<GameObject>();
 public List<GameObject> bodyParts = new List<GameObject>();

 [SerializeField]int startAmount = 3;

 public float speed = 5;
 float xDir;
 float yDir;

 bool blockW = true, blockA, blockS, blockD = false; //Block the inversed direction so you dont go backwards

 void Start()
 {
     lastWaypointPos = null;
     blockW = true;
     //Starts the Snake moving up
     xDir = 0;
     yDir = 1;
     CreateBody();
 }

 void Update()
 {
     ForceMovement();

     if(MovementInput())
     { CreateWaypoint(transform.position); }
 }

 void ForceMovement()
 {
     //Forces Forward
     transform.position += new Vector3(xDir * Time.deltaTime * speed, yDir * Time.deltaTime * speed);
 }

 private bool MovementInput()
 {
     //Movement
     if (Input.GetKeyDown(KeyCode.W) && !blockW)
     { xDir = 0; yDir = 1; blockS = true; blockA = false; blockD = false; return true; }
     else if (Input.GetKeyDown(KeyCode.A) && !blockA)
     { xDir = -1; yDir = 0; blockD = true; blockW = false; blockS = false; return true; }
     else if (Input.GetKeyDown(KeyCode.S) && !blockS)
     { xDir = 0; yDir = -1; blockW = true; blockA = false; blockD = false; return true; }
     else if (Input.GetKeyDown(KeyCode.D) && !blockD)
     { xDir = 1; yDir = 0; blockA = true; blockW = false; blockS = false; return true; }
     else
     { return false; }
 }

 void CreateWaypoint(Vector2 positon)
 {
     GameObject waypoint = Instantiate(waypointPrefab, new Vector2(positon.x, positon.y), Quaternion.identity) as GameObject;
     lastWaypointPos = waypoint;
     AddNewWaypointToBodies(waypoint);
 }

 void CreateBody()
 {
     for (int i = 0; i < startAmount; i++)
     {
         AddBodyPart(i == 0);
         bodyParts[i].transform.position -= (Vector3.up * (i + 1)) / 2;

         if(i == 0) { bodyParts[0].GetComponent<Body>().targetFollow = head; } //Sets target path to Head
     }
 }

 void AddBodyPart(bool firstBody)
 {
     GameObject body = Instantiate(bodyPrefab, transform.parent) as GameObject;
     bodyParts.Add(body);

     body.GetComponent<Body>().SetIndex(bodyParts.Count - 1);
     body.name = "body " + bodyParts.Count;

     if(!firstBody)
     {
         body.GetComponent<Body>().waypoints = bodyParts[bodyParts.Count - 2].GetComponent<Body>().waypoints;

         if (lastWaypointPos == null) {
             body.GetComponent<Body>().targetFollow = bodyParts[bodyParts.Count - 2];
         }
         else
         {
             body.GetComponent<Body>().AddWaypoint(lastWaypointPos);
         }
     }
     
 }

 void AddNewWaypointToBodies(GameObject waypoint)
 {
     for(int i = 0; i < bodyParts.Count; i++)
     {
         bodyParts[i].GetComponent<Body>().AddWaypoint(waypoint);
     }
 }

In the "Body" Script I never change its value though when I call it in the Method "SetNextPoint()" it returns 0, while in update and the inspector it returns the actual value it should be.

     public List<GameObject> waypoints = new List<GameObject>();
     public GameObject targetFollow;
     int index;
     float speed;
 
     public bool settingPoint = false;
 
     private void Start()
     {
         speed = FindObjectOfType<Player>().GetComponent<Player>().speed;
     }
 
     void Update()
     {
         Debug.Log(index + ": Update");
 
         if (!settingPoint)
         {
             if(targetFollow != null)
             {
                 if (waypoints.Count > 0 && targetFollow != waypoints[0])
                 { targetFollow = waypoints[0]; }
 
                 if (transform.position != targetFollow.transform.position)
                 { MoveToTarget(); }
                 else
                 {
                     SetNextPoint();
                 }
             }
             else
             { SetNextPoint(); }
         }  
     }
 
     private void MoveToTarget()
     {
         if (targetFollow != null)
         { transform.position = Vector2.MoveTowards(transform.position, targetFollow.transform.position, speed * Time.deltaTime); }
     }
 
     public void SetIndex(int index)
     { this.index = index; }
 
     public void AddWaypoint(GameObject newWaypoint)
     {
         waypoints.Add(newWaypoint);
     }

     //
    // RETURNS 0 IN THE METHOD BELOW
    //

     public void SetNextPoint()
     {
         settingPoint = true;
         waypoints.Remove(waypoints[0]);

         Debug.LogError(index);
 
         if (index != 0 && waypoints.Count > 0)
         { targetFollow = waypoints[0].gameObject; }
         else
         { SetTargetToPreviousBody(); }
         settingPoint = false;
     }
 
     private void SetTargetToPreviousBody()
     {
 
         Debug.LogError(index);
 
         if (index != 0)
         { targetFollow = GetComponent<Player>().bodyParts[index - 1]; }
         else { targetFollow = FindObjectOfType<Player>().gameObject;  }       
     }
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

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

235 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 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

Problem with int values + 1 gives unexpected values 1 Answer

List.FindIndex 1 Answer

(17,37): error CS0841: A local variable `spawnPointIndex' cannot be used before it is declared 1 Answer

Declared variable being returned as null in a method? 0 Answers

How to make a number higher than Quintillion? (1000000000000000000) 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