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 Sarmale32 · Apr 17, 2017 at 02:08 PM · updatefunctioncalling

Why doesen't the function get called the second itme?

 So I am trying to spawn some GameObjects in game based on the current player position, essentially trying to make an infinite runner type of game...I made a function `SpawnPylon()` so I can spawn the pylons with it but for some reason the function get's called only once per frame, It does not get called the second time with a different parameter.
 Why does the second call of the function not work? 

This is my code:

     public class CameraScript : MonoBehaviour {
 public float cameraSpeed = 1;
 public float horizontalSpeed;
 private int spawnIndex;
 public float spawnNormPylonDis;
 public float spawnCoinPylonDis;
 private int currPosition ;
 public GameObject[] pilons;
 public GameObject spawnMainPoint;
 public Transform[] spawnPoints;
 public Transform[] coinsSpawnPoint;
 public float enamySpeed;
 private int currentPoint;
 public Transform[] pointPosition;

 // Use this for initialization
 void Start () {
     //This spawns the Pilons.
     spawnMainPoint.transform.position = pointPosition [0].position;
     currPosition = (int) transform.position.z;

 }
 // Update is called once per frame
 void FixedUpdate () {

     spawnMainPoint.transform.position = Vector3.MoveTowards (spawnMainPoint.transform.position, pointPosition[currentPoint].position, Time.deltaTime * horizontalSpeed);

     SpawnPylon (pilons[1],spawnPoints,spawnNormPylonDis,"Check function");
     SpawnPylon (pilons [0], spawnPoints, spawnCoinPylonDis,"Check the second function");

     GetComponent<Rigidbody> ().velocity = transform.forward * cameraSpeed;

     //the next if statements make the Pilons spawn randomly and corectly.
     if (spawnMainPoint.transform.position == pointPosition [currentPoint].position) {
         currentPoint++;
     }
     if (currentPoint == pointPosition.Length) {
         currentPoint = 0;
     }

 }
 /*This function spanws the a GameObject randomly at a GameObject's position and it takes 2 arguments :
 Argument 1: type GameObject 
          2: type Transform[]*/
 void SpawnPylon (GameObject whatSpawn,Transform[] whereSpawn,float spawnDistance,string plm)
 {        
     bool hasSpawnedPylon = false;

     if (currPosition != (int)transform.position.z)
     {
         if ((int)transform.position.z % spawnDistance == 0) 
         {
             Debug.Log (plm);
             if (!hasSpawnedPylon) 
             {
                 //this makes the GameObject spawn randomly
                 spawnIndex = Random.Range (0, spawnPoints.Length);
                 //This is instantiationg the GameObject
                 Instantiate (whatSpawn, whereSpawn [spawnIndex].position, whereSpawn [spawnIndex].rotation);
                 //this makes shore that the GameObject is not spawned multiple times at aproximetley the same position.
                 currPosition = (int)transform.position.z;
             }
         }
     }
     else
     {
         hasSpawnedPylon = false;
     }            
 }

}

Here I have a picture with the script in the inspector:

alt text

And here is the console, trying to figure it out by using Debug.Log () for the calls of the function.alt text

inspector.png (268.5 kB)
check-picture.png (133.6 kB)
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
1

Answer by bobisgod234 · Apr 19, 2017 at 02:48 AM

Once you successfully spawn one pylon, you set your currPosition variable to be the transforms Z position, so your if condition

 if (currPosition != (int)transform.position.z)
 

Will prevent you from spawning another until the gameObject with your "CameraScript" component moves along the Z axis by some amount.

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 NoseKills · Apr 19, 2017 at 06:13 AM 0
Share

@Sarmale32 in a more general sense, theres 2 if conditions just before the Debug.Log line that doesn't get executed. Even if you can't find the problem by reading your code, placing more Debug.Logs outside the if-clauses to see the values of your conditions would help you find the issue. Basic debugging :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Call a function once from OnGUI 2 Answers

touch buttons function OnGUI issue 1 Answer

How to call functions? 1 Answer

Function isn't being called 1 Answer

Aiming with function "Update" not working 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