Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ruimcp95 · Jun 04, 2020 at 03:49 PM · vector3runtimeinstantiate prefabchild objectstatic variable

Static value “lost” in Unity when called on second script

I'm trying to implement a system with platforms and OntriggerExit that spawn new platforms. The objects that contain the trigger function are children to said platforms. I.e, I have 4 triggers as children in each platform. I'm trying to use the trigger object's global position to instantiate the new platform but it keeps doing so according to the prent's position. For instance, the parent (platform) is at (0,0,0). The trigger, child to the platform, is at (-15,0,0) globally. I try to add (-35,0,0) and it instantiates at (-35,0,0) instead of (-50,0,0). I do this with a static value. I obtain the Trigger position when OnTriggerExit() and then use it on a different script as follows:

 public class TriggerExitN : MonoBehaviour
 {
     public delegate void ExitAction();
     public static event ExitAction OnChunkExitedN;
 
     public static Vector3 positionN;
 
     private bool exited = false;
 
     private void OnTriggerExit(Collider other)
     {
         Player player = other.GetComponent<Player>();
         if (player != null)
         {
             if (!exited)
             {
                 exited = true;
                 OnChunkExitedN();
 
                 positionN = gameObject.transform.position;
 
             }        
         }
     }
 }

If I debug positionN here it returns (-15,0,0) as expected.

Then the platform generator:

 public class MapGenerator : MonoBehaviour
 {
     public GameObject[] mapprefabs;
     public GameObject[] startprefabs;
     private List<GameObject> platformslist = new List<GameObject>();
 
     public Vector3 spawnOrigin;
 
     private Vector3 spawnPosition;
     // Start is called before the first frame update
     void Start()
     {
         platformslist.Add(startprefabs[0]);
     }
 
  void OnEnable()
     {
         TriggerExitN.OnChunkExitedN += PickAndSpawnN;
     }
 
 private void OnDisable()
     {
         TriggerExitN.OnChunkExitedN -= PickAndSpawnN;
     }
 
 void PickAndSpawnN()
     {
         spawnPosition = TriggerExitN.positionN + new Vector3(-35f, 0, 0f);
 
         foreach (GameObject platform in platformslist)
             if(platform.transform.position == spawnPosition)
             {
                 return;
             }
             else
             {
                 continue;
             }
 
         GameObject objectFromChunk = mapprefabs[Random.Range(0, mapprefabs.Length)];
 
         Instantiate(objectFromChunk, spawnPosition + spawnOrigin, Quaternion.identity);
 
         platformslist.Add(objectFromChunk);
 
         Debug.Log(TriggerExitN.positionN);
     }

If I debug poisitionN here, it returns (0,0,0).

I even tried to switch positionN to the MapGenerator script, since there's only one instance of it in the scene. But to no avail. The same behaviour happens: (-15,0,0) debugged on the TriggerExitN and (0,0,0) debbugged on MapGenerator. As new platforms are instanciated the next positions for the platforms end up being (-50,0,0) , (-85,0,0), (-100,0,0), (-135,0,0).

I know I'm missing something, I'd just like some help with it because it's driving me mad. Please disregard the rest of the code that does not concern the value and usage of positionN for it is not yet well articulated. Thank you in advance.

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 ruimcp95 · Jun 04, 2020 at 12:23 AM 0
Share

Never$$anonymous$$d. Rookie mistake. In my blind rage I did not notice that`OnChunkExitedN();` should come after positionN = gameObject.transform.position;. Someone already pointed it out for me. I feel so dumb. Thank you anyways.

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

234 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

Related Questions

How to calculate the vertices of an icosphere 1 Answer

Changing prefab of a GameObject at runtime C# 1 Answer

How to use Animator.Rebind ??? 0 Answers

Change value of a static variable multiple times in one script 2 Answers

Menu Component/GoogleVR/Update Stereo Cameras can't be checked because doesn't exist. 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