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!
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.
-
Create an empty gameobject named “Whatever”
-
Attach the MoveCubes script to the “Whatever” game object.(by drag and drop the script to the game object)
-
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
}
}
}
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! 