|
I just need a term so I can look up examples. I'm looking at manipulating variables stored in another script. Here's a basic example of what I'm trying to accomplish. store.cs using UnityEngine; using System.Collections; updater.cs using UnityEngine; using System.Collections; So if a player hits the up key, num1 adds + 1 to itself, and the down key will subtract one from itself. Again, if anyone knows the tem for this, I'll be happy to look it up myself. Thank you kindly!
(comments are locked)
|
|
This is actually pretty simple. You just need to make sure the scripts are actually attached to objects in the current scene, then you can reference then. I created a sample scene for you... Create a new Test Project in Unity and open the scene. Clicking Arrow up in the "player object" and you will see the "Score" variable going up in the ScoreManager object. Thank you for that! That's a great tool. Hopefully others will be able to see how it works also. Thanks again!
Aug 29 '12 at 03:14 AM
GlitchBait
(comments are locked)
|
|
(comments are locked)
|
|
It looks like you want to use static variables (and also make them public). However, keep in mind that a static variable is the same for all instances of the class, so if you had 2 GameObjects with the "store" component, they'd both reference the same variable. The code would look like this: If you have more than 1 "store" GameObject, you could create a property to manipulate the values. If you do that, you'll need to get a reference to your GameObject in your updater.cs script. You could do this by adding the following code at the top: Then, set the value of myStore in your unity scene. Then, later in the script use the myStore variable to change the values.
(comments are locked)
|

The keyword "using" is point less in your case. You usually use it to include "namespaces" so you don't have to write it all the time. You should learn the difference between a class or script and an instance of such a class. At runtime you always work with instances.
In the case of Unity's component concept the instances are always attached to a GameObject. That's a strategy-like pattern
Eric posted the most important link on this topic in Unity ;)
I'm very greatful for all of the comments and suggestions. Thank you kindly.