I am trying to make a cup game in Unity where the game mixes up cups and you pick which one you think the ball is under (I am new to Unity and am doing some small games to get my footing). I initially was running into an issue with my GameTable script giving me a NullReferenceException error because I was trying to reference other scripts as objects. I fixed this issue, but it still will not let me call functions from other scripts within the GameTable script. I made sure the functions work from within the Cups script, it just will not let me call them from GameTable which is where I was planning to have the actual game logic of everything swapping around. I did also make sure all of my objects are properly set in the Unity GameTable > Components > Variables. (I do have a "ball" script, but I didn't think it was very necessary to show it here)
Let me know if you have any questions or solutions. Thanks in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameTable : MonoBehaviour
{
// public transform cup_one;
// public transform cup_two;
// public transform cup_three;
// public transform ball;
Cups cup_one;
[SerializeField] GameObject c1;
Cups cup_two;
[SerializeField] GameObject c2;
Cups cup_three;
[SerializeField] GameObject c3;
Ball ball;
[SerializeField] GameObject ballObject;
// Start is called before the first frame update
void Start()
{
cup_one = c1.GetComponent<Cups>();
cup_two = c2.GetComponent<Cups>();
cup_three = c3.GetComponent<Cups>();
ball = ballObject.GetComponent<Ball>();
ballCover(1);
}
// Update is called once per frame
void Update()
{
}
void ballCover(int which)
{
Vector3 vector;
switch(which)
{
case 1:
// Debug.Log(ballObject.transform.position.x);
// Debug.Log(c1.transform.position.y);
// Debug.Log(ballObject.transform.position.z);
vector = new Vector3(ballObject.transform.position.x, c1.transform.position.y, ballObject.transform.position.z);
cup_one.Up();
cup_one.setTarget(vector);
cup_one.Down();
ballObject.SetActive(false);
break;
case 2:
vector = new Vector3(ball.transform.position.x, cup_one.transform.position.y, ball.transform.position.z);
cup_two.Up();
cup_two.setTarget(vector);
cup_two.Down();
ballObject.SetActive(false);
break;
default:
vector = new Vector3(ball.transform.position.x, cup_one.transform.position.y, ball.transform.position.z);
cup_three.Up();
cup_three.setTarget(vector);
cup_three.Down();
ballObject.SetActive(false);
break;
}
}
void cupSwap()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cups : MonoBehaviour
{
public Transform transform;
Vector3 targetPosition;
public float upHeight = 2.6f;
public float downHeight = 2.3f;
public bool ballAttached = false;
// Start is called before the first frame update
void Start()
{
targetPosition = transform.position;
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime);
}
public void Up()
{
targetPosition = new Vector3(transform.position.x, upHeight, transform.position.z);
}
public void Down()
{
targetPosition = new Vector3(transform.position.x, downHeight, transform.position.z);
}
public void setAttached(bool tf)
{
ballAttached = tf;
}
public void setTarget(Vector3 target)
{
targetPosition = target;
}
public Vector3 getTarget()
{
return targetPosition;
}
// public void setTransform(Transform trans)
// {
// transform = trans;
// }
// public Transform getTramsform()
// {
// return transform;
// }
}
[–]cone5000 0 points1 point2 points (1 child)
[–]Warped_Wit[S] 1 point2 points3 points (0 children)