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 Loius · Jun 24, 2012 at 10:33 PM · publicprivate

Variable value persists when public but is lost between frames when private

I am at a complete loss. I have no idea how this is happening. Here's my PathFollower object:

 class PathFollower extends AI { // And AI extends Ship
     var curve : float = 1.0;
 
     private var targetNode : Node;
 
     function StartFollowing( node : Node ) {
         targetNode = node.RandomNext();
         if ( !targetNode ) {
             gameObject.SetActiveRecursively(false);
         }
     }
 
     function Update() {
         super.Update();
         if ( !targetNode ) { return; }
         if ( (targetNode.transform.position - transform.position).sqrMagnitude < 0.3 ) {
             StartFollowing(targetNode);
             
         }
         HomeTowards( targetNode.transform.position );
     }
 }

To create one of these, I do only this:

 private function Trigger() {
     count--;
     var ship : Ship = spawner.Make( transform.position ).GetComponent(Ship);
     var follower : PathFollower = ship.GetComponent(PathFollower);
     if ( targetNode && follower ) {
         follower.transform.position = targetNode.gameObject.transform.position;
         follower.StartFollowing(targetNode);
     }
     if ( count <= 0 ) CancelInvoke(); //gameObject.active = false;
 }

Now we get to the extremely fun part.

I originally build a simple Spawner that has just the code above and some timing stuff, and it works perfectly.

So I built my LevelHandler which controls spawns and waves and stuff, and just copied the code into that. Here's the complete, new function:

 public function Process() {
     if ( remain <= 0 ) return;
     next-= Time.deltaTime; 
     
     if ( next <= 0 ) {
         next+= rate;
         remain--;
         
         var s : Ship = spawner.Make( node.transform.position ).GetComponent(Ship);
         var follower : PathFollower = ship.GetComponent(PathFollower);
         if ( node && follower ) {
             follower.StartFollowing(node);
         }
         s.AddListener(this);
     }
 }

This spawns the Ship. This gives the Ship a node to follow - using Debug statements, I can see that startFollowing is not only receiving a node, but it's also setting targetNode to something meaningful. And yet, in Update (even before super.Update), targetNode is null, meaning the Ship just sits still.

Process is called during an Update function. Trigger is called as a result of Invoke.

The original Trigger function still works correctly. Changing targetNode to public makes Process work correctly.

Protected behaves just like Private.

What is going on?

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
Best Answer

Answer by whydoidoit · Jun 24, 2012 at 10:40 PM

You appear to be getting a path follower from ship - but you have spawned a thing stored in s. I'm guessing that ship is the prefab instance or something - the reason it works when it is public is that the instantiate will copy a public variable, but not a private one unless it is marked with @SerializeField.

Will s.GetComponent(PathFollower) do it for 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 Loius · Jun 25, 2012 at 01:25 AM 0
Share

Haha.

Yes.

I cannot type.

For all you following along at home there's a stray "ship." up in there, which should have been "s."

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

When to use public or private variables for the inspector? 2 Answers

Has anyone had success with Facebook's graph API in Unity's Flash export? 1 Answer

Changing variable values not having any effect 2 Answers

Explanation on how to use functions, variables from other files 1 Answer

How faster is private variables over public variables? 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