Inspector (for ball sprite)Trying to add the Ball script to "Ball"I am creating a BlockBreaker game 2D and whenever I try to connect a script to a sprite it gives me the error: "Can't add script behaviour VisualContainerAsset.The script needs to derive from MonoBehaviour."
I have 6 scripts on total and when I try to attach one to a sprite they each give me the same error. Can someone tell me why?
SCREENSHOT OF ERROR
This is the ball script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour {
public Paddle paddle;
bool hasStarted = false;
Vector3 paddleToBallVector;
void Start () {
paddleToBallVector = this.transform.position - paddle.transform.position;
}
// Update is called once per frame
void Update () {
if(hasStarted == false)
{
this.transform.position = paddle.transform.position + paddleToBallVector;
if (Input.GetMouseButtonDown(0))
{
hasStarted = true;
this.GetComponent<Rigidbody2D>().velocity = new
Vector2(Random.Range(-1f, 3f), 10f); //This is for the ball to bounce randomly
}
}
}
void OnCollisionEnter2D(Collision2D myColl)
{
if (myColl.gameObject.name == "Rightborder" || myColl.gameObject.name == "Leftborder") //If the ball hits either the left border or the right border
{
this.GetComponent<Rigidbody2D>().velocity += new Vector2(0f, 1f); //When the ball hits one of the borders, the speed will not be reduced
}
}
}
The name of the file and the class need to be consistent. I.e. if the class is called Ball, the file should be called Ball.cs or Ball.script, I forget what they're saved as.
If that doesn't work, I found this on the unity forums:
Apparently you need to reset the script?
Related
I am a beginner in Unity and I am currently making a simple game. I have a problem where the instantiated Button is not destroyed.
The process of the script is to instantiate a button and destroy it upon collision exit. But when I move out of the object, the object stays and it is not destroyed. i don't know if there is something wrong with the Destroy code of line.
Here is the script for the collision:
using UnityEngine;
using UnityEngine.UI;
public class InteractButtonPosition : MonoBehaviour
{
public Button UI;
private Button uiUse;
private Vector3 offset = new Vector3(0, 0.5f, 0);
// Start is called before the first frame update
void Start()
{
//uiUse = Instantiate(UI, FindObjectOfType<Canvas>().transform).GetComponent<Button>();
}
// Update is called once per frame
void Update()
{
//uiUse.transform.position = Camera.main.WorldToScreenPoint(this.transform.position + offset);
}
private void OnCollisionEnter(Collision collisionInfo)
{
if(collisionInfo.collider.name == "Player")
{
uiUse = Instantiate(UI, FindObjectOfType<Canvas>().transform).GetComponent<Button>();
uiUse.gameObject.SetActive(true);
uiUse.transform.position = Camera.main.WorldToScreenPoint(this.transform.position + offset);
}
}
private void OnCollisionExit(Collision collisionInfo)
{
if(collisionInfo.collider.name == "Player")
{
Destroy(uiUse);
}
}
}
The code does exactly what you have written, which is destroying uiUse which is a Button component. If you go and inspect your game object in the scene, you will see, that indeed, the button component has been destroyed.
What you want to do is to destroy the GameObject the button component is attached to, like: Destroy(uiUse.gameObject)
Hello so I've been looking for a solution for my problem but looks like there is absolutely nothing about it.
I'm working on a scene where I have some 3D object renderred on a ground plane and my goal is making an animation on that 3D object start by tapping it. I'm using latest version of vuforia 10.4 with Unity 2020.3.9f1. I have a script for instantiating the 3d Model and making the plane finder disappear as long as it doesn't lose tracking.`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sceneManager : MonoBehaviour
{
private string level = "snake";
public GameObject[] renderredPrefab;
public GameObject ground;
public GameObject groundFinder;
private int levelChecker(string levelName)
{
if (levelName == "snake")
return 0;
else return 1;
}
public void spawnObject(int i)
{
Instantiate(renderredPrefab[levelChecker(level)], new Vector3(0, 0, 0), Quaternion.identity, ground.transform);
}
public void groundFinderOff()
{
groundFinder.SetActive(false);
}
public void groundFinderOn()
{
groundFinder.SetActive(true);
}
}
And another one to trigger the animation following the game object's tag hereusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class animationTriggerManager : MonoBehaviour
{
private Animator m_Animator;
private string objectName;
private GameObject[] eos;
private GameObject snake;
[SerializeField]
private Camera ARCamera;
// Start is called before the first frame update
void Start()
{
// Get the different eos present on the scene
for (int i = 0; i < eos.Length; i++)
{
eos[i] = GameObject.FindWithTag("myEolienne" + i);
}
// Get snake game objecct in the scene
snake = GameObject.FindWithTag("Snake");
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
Ray ray = ARCamera.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray, out RaycastHit hit))
{
objectName = hit.collider.name;
Debug.Log("raycast touched " + hit.transform.name);
switch (objectName) //Get the Animator depending on which gameObject been tapped on.
{
case "myEolienne1":
m_Animator = eos[0].GetComponent<Animator>();
// Launch the animation on the gameObject that's been tapped
m_Animator.SetTrigger("Helice_Rotate");
Debug.Log("rotate launched");
break;
case "myEolienne2":
m_Animator = eos[1].GetComponent<Animator>();
// Launch the animation on the gameObject that's been tapped
m_Animator.SetTrigger("Helice_Rotate");
Debug.Log("rotate launched");
break;
case "myEolienne3":
m_Animator = eos[2].GetComponent<Animator>();
// Launch the animation on the gameObject that's been tapped
m_Animator.SetTrigger("Helice_Rotate");
Debug.Log("rotate launched");
break;
case "Snake":
m_Animator = snake.GetComponent<Animator>();
m_Animator.SetTrigger("snakeMoving");
break;
}
}
}
}
}
`
Note that each 3D model has different parts grouped in one parent that has a mesh collider on the parent only.enter image description here
The rendering works perfectly but I can't figure out what's wrong with my raycasting script. Note that I first tried with 3D model on image target and it worked fine.
Thanks in advance !
Hello im new at AR development, i've tried to make a simple script that takes the camera position and the position of some object in the world and then draw a simple line between them using LineRenderer.
The object is tied to an imagetarget and im running with the FIRST_TARGET setting (so the world should have its origin (0,0,0) on the imagetarget)
However the camera position seems to be wrong for me and i dont really understand why. What i want is a line from the center of the mobile (Camera) screen to some object in the world viewed using an imagetarget. I would love some help with my issue. The code for my script is here:
public class POILine : MonoBehaviour
{
//Camera
private GameObject camera;
//Gameobject of interest to point the user towards
public GameObject to;
private GameObject toParent;
private LineRenderer line;
public float lineWidthMultiplier = 0.5f;
private float distance;
public float distanceMin = 50f;
void Start ()
{
camera = GameObject.FindGameObjectWithTag("MainCamera");
line = this.gameObject.AddComponent<LineRenderer>();
line.positionCount = 2;
//Thickness of the line
line.widthMultiplier = lineWidthMultiplier;
}
// Update is called once per frame
void Update ()
{
TrackableBehaviour.Status toStatus = to.transform.parent.GetComponent<TrackableBehaviour>().CurrentStatus;
if (toStatus == TrackableBehaviour.Status.TRACKED || toStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
distance = Vector3.Distance(camera.transform.position, to.transform.position);
if (distance > distanceMin)
{
line.enabled = true;
if (to != null)
{
//I think the error is here
line.SetPosition(0, camera.transform.position);
line.SetPosition(1, to.transform.position);
}
}
else
{
line.enabled = false;
}
}
}
Managed to get it working, what i did was that i placed an empty gameobject as a child to the Imagetarget i was currently tracking, then i made it follow the ARCamera transform position.
Then the code works by rendering a Line from that empty gameobject to the object of interest.
I'm a complete unity novice. I want to make a simple scene where you have three lives and you lose a live if you collide with a cube. This is my script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Lives : MonoBehaviour {
public Transform player;
public static int lives;
public Image live1;
public Image live2;
public Image live3;
// Use this for initialization
void Start () {
lives = 3;
live1.enabled = true;
live2.enabled = true;
live3.enabled = true;
}
void Update () {
DisplayOfHearts();
}
public static void Damage() {
lives -= 1;
}
public void OnCollisionEnter(Collision col) {
if (col.gameObject.tag == "cube") {
Lives.Damage();
}
}
public void DisplayOfHearts() {
if (lives == 2) {
live3.enabled = false;
}
else if (lives == 1) {
live2.enabled = false;
}
else if (lives == 0) {
live1.enabled = false;
}
}
}
What happens is the player can't move through the cube but the amount of lives stays three. Is there something I'm missing?
The problem is you have attached the script to a wrong game object. The script and the collider must be attached to the same game object.
Unity methods inside a MonoBehaviour script (such as OnEnable, Update, FixedUpdate, Awake, Start, OnTriggerEnter, OnCollisionStay, etc..) only work for the game object which the script is attached to.
If you attach the script to another game object don't expect any of those to work. Update only works while that game object is active. OnCollisionEnter only works when a collision occurs on a collider which is attached directly to that game object. (it doesn't even work when a child has the collider instead of the actual game object where script is attached to)
I'm totally new with Unity (and game development in general). I followed the great simple tutorial Survival Shooter and I have one question: in this tutorial, we add a Y constraint position to the rigidbody's character plus we set the drag value and the angular drag value to infinite. How can we make the character jump since those settings prevent the character from moving to the Y axis?
If someone can give me an hand on that please...
Thanks a lot!
Why exactly do you add the constraint on the Y axis? You could remove it, and then just add gravity that will make your player stick to the ground. After that just apply a force, or just a simple translation going upwards by a set speed, to make the player jump and then wait for gravity to bring him back down.
This is what I would do to jump.
P.S. you need to remove the constraints on the Y axis on the vector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public Vector3 force;
public Rigidbody rb;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Space) && transform.position.y == 0) //Enter your y axix where ground is located or try to learn a little more about raycasting ill just use 0 for an example)
{
rb.AddForce(force);//Makes you jump up when you hold the space button down line line 19 will do so that you only can jump when you are on the ground.
} if (Input.GetKeyUp(KeyCode.Space))
{
rb.AddForce(-force); //When you realase the force gets inverted and you come back to ground
}
}
}
I would do this instead edit the code over this post.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Just : MonoBehaviour {
public Vector3 force;
public Rigidbody rb;
bool isGrounded;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true) //Rember to got to your "Ground" object and tag it as Ground else this would not work
{
rb.AddForce(force);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
isGrounded = false;
}
}
You need to assign your ground object a tag called Ground, You need to make your own tag called Ground its not that hard tho you click your object and top left of the inspector there's the tag and then u just make a new tag called Ground. And also plz rember to assign the other values on your player object.