- Home /
My variables are storing different values?
I feel like this is really simple, but for the life of me can't figure it out. I created a class called Triangle that stores three points. Whenever I create a new triangle, the values that are stored are not the ones I give it. For instance, here is the debug log after I store Vector3.up, Vector3.down, and Vector3.forward:
Here's my code:
public class TriangulatePoints : MonoBehaviour
{
[SerializeField] GameObject[] nodes = new GameObject[3];
[SerializeField] Triangle triangleOne;
// Start is called before the first frame update
void Start()
{
triangleOne = new Triangle(Vector3.up, Vector3.down, Vector3.forward);
Debug.Log("Triangle point one " + triangleOne.pointOne);
Debug.Log("Triangle point two: " + triangleOne.pointTwo);
Debug.Log("Triangle point three " + triangleOne.pointThree);
}
// Update is called once per frame
void Update()
{
}
}
public class Triangle : MonoBehaviour
{
public Vector3 pointOne;
public Vector3 pointTwo;
public Vector3 pointThree;
public Triangle(Vector3 firstPoint, Vector3 secondPoint, Vector3 thirdPoint)
{
pointOne = firstPoint;
pointTwo = secondPoint;
pointTwo = thirdPoint;
}
}
You must not derive your Triangle class from $$anonymous$$onoBehaviour. $$anonymous$$onoBehaviours are components which have to be attached to a gameobject. Since you defined a constructor and create an instance with new it must not be a $$anonymous$$onoBehaviour.
If you want it to be a component you can't use a constructor and you can not use "new" to create an instance. To create a $$anonymous$$onoBehaviour dynamically you have to use .AddComponent<Triangle>()
on a gameobject. Also $$anonymous$$onoBehaviour derived classes need to be placed in their own script file and the filename has to match the classname.
Thanks for your reply! I had Triangle in a separate script, but I can't seem to get spacing to work properly in Unity Answers so it just showed up as one. Good to know that about $$anonymous$$onobehaviours.
Answer by unity_ek98vnTRplGj8Q · Mar 12, 2020 at 07:22 PM
pointOne = firstPoint;
pointTwo = secondPoint;
pointTwo = thirdPoint;
Something is fishy here... or should I say ... fishThree ....
Your answer
Follow this Question
Related Questions
Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers
Loading external variables via SendMessage into Unity WebPlayer 2 Answers
GetUnity() on webplayer not working properly 1 Answer
How should a Unity web application gracefully exit? 1 Answer
Application.targetFrameRate limited to 60 fps on webplayer 0 Answers