1
\$\begingroup\$

I have four similar objects in my game. As they are all similar therefore I have created a single script that defines mash for all of them. The script that defines mash for these objects uses a variable called radius which I have imported from other file (by classname.radius). The radius updates on every frame and it is the only property that is different for all objects.

To allow different radius for different objects, What I thought would work was, making four different classes of same name classname. Creating variable radius in all of them and giving them different values. Dragging first file to the first object, second file to second object and so on.. When executed, every time the script that generated mash considers classname.radius from the file which is dragged to that object. But that isn't working. What is wrong and how can I implement it?

Seems I'm messing with OOPS concepts!

\$\endgroup\$
  • 3
    \$\begingroup\$ It seems you should try learn the basics of C# (or whatever language you're using for Unity) first. This is a basic question of scope, classes and objects. \$\endgroup\$ – Gustavo Maciel Jun 3 '15 at 12:08
1
\$\begingroup\$

You only need one class with one non static public member 'radius' (static variables wont work...)
Just create several instances of this class and connect them to correct meshes. You can have an empty gameObject to hold those instances, then drag and drag them to connect them to your other script on your meshes.

There are other ways to implement this though, but all require you to create class instances...

\$\endgroup\$
0
\$\begingroup\$

The script that defines mash for these objects uses a variable called radius which I have imported from other file (by classname.radius).

So this means your variable radius is static? But since you want it to be different for every instance of your object, why do you have it as a static variable? Why not just make the variable not tied to the class, but to the instance of the class.

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.