- Home /
Duplicate Question
Help with more code
have a new problem with the first code, I dont know what a static member is.
Error: Assets/Scripts/WightedCube.cs(9,35): error CS0120: An object reference is required to access non-static member `MoveCubes.Movecubes()'
Code 1 : WeightedCube(for weighted cube):
using UnityEngine;
using System.Collections;
public class WightedCube : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Pad") {
MoveCubes.Movecubes();
}
}
}
code 2 : MoveCubes (the code that makes the cubes move):
using UnityEngine;
using System.Collections;
public class MoveCubes : MonoBehaviour {
public void Movecubes(){
Vector3 posCopy = tranform.position; // make a copy with all the previous values for x/y/z.
posCopy.y = 1; // make modification
transform.position = posCopy; // change position to now copy.
}
}
if you can help that would be great!
Imagine you have an office of people and they are all doing work and communicating. There is a noticeboard with a piece of paper on it that has a piece of information that everyone needs and uses. No person can simply change the number just for themselves as it needs to be the same as the rest. He can change the one on the paper which changes it for everyone.
The information on that paper is static. It can be accessed by anyone and is unique.
You dont need to reference the object to access the static variable as everyone can clearly already see the noticeboard.
However, now I want to read the information from someones desk. I need to call out to that desk's worker for the information.
That's my reference; the communication with the worker. He can find the bits of info I ask for. Its what GetComponent is for.
Answer by hangemhigh · Apr 19, 2015 at 01:36 PM
You are trying to access another function from another class. You can't do MoveCubes.Movecubes();
unless the function you want to call from another class is static. Example of a static function is as below.
public static void Movecubes(){
Vector3 posCopy = tranform.position; // make a copy with all the previous values for x/y/z.
posCopy.y = 1; // make modification
transform.position = posCopy; // change position to now copy.
}
Now you can do MoveCubes.Movecubes(); This will work for c#. In unity, it is different. To access another function without any errors using Unity, the script must be attached to a game object. To get this to work.
1) Create an empty gameobject named "Whatever"
2) Attach the MoveCubes script to the "Whatever" game object.(by drag and drop the script to the game object)
3) In your WightedCube class, change
public class WightedCube : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Pad") {
MoveCubes.Movecubes();
}
}
}
to
public class WightedCube : MonoBehaviour {
private MoveCubes myMovesCubes;
void Awake(){
/*Find the "Whatever" game object we created then get the "MoveCubes" script attached to it*/
myMovesCubes = GameObject.Find("Whatever").GetComponent<MoveCubes>();
}
void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Pad") {
myMovesCubes.Movecubes();//call the function using the reference
}
}
}
Answer by admanic · Apr 19, 2015 at 02:05 PM
This is a problem with how you are using the methods.
In an Object Oriented Programming (OOP) language, Classes are like blueprints that can be used by objects for them to know what they have (variables) and what they do (methods). In order to execute the method, MoveCubes, an instance of the class needs to exist. What you are currently doing is referencing the class as a whole and trying to execute a static method, which is a method that can be execute by anything at any time without having to have a specific object of that type.
What you need to do is find the script or the object that you want to execute the method, in this case I assume you have one gameobject with these two scripts on them. On the WightedCube script you can do a few things:
Create a public variable on the script and drag the MoveCubes script from wherever it is in your scene to that public variable.
public MoveCubes m_moveCubes;
then in your OnTriggerEnter script where you have "MoveCubes.MoveCubes(); change that to:
m_moveCubes.MoveCubes();
The second part is the key part because there you are referencing an actual instance of the class that exists rather than the class as a whole.
If the MoveCubes script is on the same gameobject as the WightedCube script you can also get the MoveCubes script by using:
m_moveCubes = GetComponent<MoveCubes>();
This is simply a misunderstanding with a concept of OOP languages. Essentially what you are doing is rather than asking a specific car to turn the engine on, you are asking the manufacturers blueprints of that type of car to do it. Hopefully this helps, it's quite a hard concept to explain through text! :)
I would also recommend not using the same method name as the class name, this can get really confusing. So change the method $$anonymous$$oveCubes() to moveCube() or something.
Also WightedCube, i'll assume is a spelling mistake which should be "WeightedCube".
Just small things but they help organise your code!
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Error Code CS0118 or CS0120 1 Answer
Multiple Cars not working 1 Answer
Sync with visual studio 2010 error 1 Answer
No Monobehaviour scripts in files 1 Answer