x


C# programming term needed

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;

public class store : MonoBehaviour {


    //Variables

    private int num1;



    //Variables - END




    // Use this for initialization
    void Start () {

    }

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

    }
}

updater.cs using UnityEngine; using System.Collections;

using store;


public class Updater : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

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


       if (Input.GetKey("up"))
       {

         store.num1 = store.num1 + 1;
          Debug.Log("Num1 = " + store.num1);

       }


       if (Input.GetKey("down"))
       {
         store.num1 = store.num1 - 1;
          Debug.Log("Num1 = " + store.num1);

       }

    }
}

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!

more ▼

asked Aug 28 '12 at 09:50 PM

GlitchBait gravatar image

GlitchBait
138 1 5 11

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 ;)

Aug 28 '12 at 11:54 PM Bunny83

I'm very greatful for all of the comments and suggestions. Thank you kindly.

Aug 29 '12 at 03:01 AM GlitchBait
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.
In your scripts make sure the variables you are trying to expose are public. I can see in your example above, your "num1" variable in the "store" script is private, that would not work even if you referenced correctly.. make your variables public when needed.

Download test Scene here

more ▼

answered Aug 29 '12 at 12:45 AM

fscopel gravatar image

fscopel
34 1

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)
10|3000 characters needed characters left
more ▼

answered Aug 28 '12 at 10:46 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

(comments are locked)
10|3000 characters needed characters left

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:

public static int num1;

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:

public store myStore;

Then, set the value of myStore in your unity scene. Then, later in the script use the myStore variable to change the values.

more ▼

answered Aug 29 '12 at 12:45 AM

lukegravitt gravatar image

lukegravitt
18 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x356

asked: Aug 28 '12 at 09:50 PM

Seen: 242 times

Last Updated: Aug 29 '12 at 03:14 AM