I'm trying to get a camera to move whenever you press either WASD or the arrow keys, but it's throwing
Error CS0428 Cannot convert method group 'GetComponent' to non-delegate type 'Transform'. Did you intend to invoke the method?
On this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameramover : MonoBehaviour
{
public Camera controlled;
Vector3 movement;
void Start()
{
Transform transform1 = controlled.GetComponent<Transform>;
}
void Update()
{
movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
}
}
I tried invoking the method, but that threw CS0201, so I tried doing
new Transform(controlled.GetComponent<Transform>;)
and that didn't work either, so I went here to ask for help.
Figured it out with the help of the wonderful people at the Game Dev Network discord server! i just had to add a pair of parentheses on the end of the controlled.GetComponen;
Related
Why the sound effect does not work in unity?
when I try to add a sound effect, it doesn't work
This is my script to move to the next scene using buttons:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelLoader : MonoBehaviour
{
public Animator transition;
public float transitionTime = 1f;
public void LoadNextLevel()
{
StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex + 1));
}
public AudioClip impact;
IEnumerator LoadLevel(int LevelIndex)
{
transition.SetTrigger("Start");
yield return new WaitForSeconds(transitionTime);
SceneManager.LoadScene(LevelIndex);
AudioSource.PlayClipAtPoint(impact, transform.position);
}
}
You're making the audio play, and then immediately loading in a different scene. The object that is playing the audioclip also unloads.
You could offset the two by making NewGame() a coroutine instead, with a small timed offset between the PlayClipAtPoint() call and the LoadScene() call.
As Alex Leest said, you are loading new scene right after audio play.
When unity scene loading started, all gameobject except DontDestroyOnLoad Object are destroyed.
So, you may add code below to your Audio Manage class.
(Also, you should ensure that your Audio Manager class always has only one instance in your whole program for not cause bugs)
private void Awake()
{
DontDestroyOnLoad(this);
}
So I'm using this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class wands : MonoBehaviour
{
public Transform WandPos;
void Update()
{
WandPos.rotation = Quaternion.Lerp(new Quaternion(0,0,1,0), new Quaternion(0,0,0.866025448f,0.49999994f),Time.time * .5f);
}
}
For some reason it is preforming the rotation before the GameObject even rotates, so when it instantiates, the GameObject snaps to where it would be if it instantiated when the scene started. I'm probably just missing something obvious, but I can't seem to find my solution anywhere online either.
Also, if there is an easier way to do this than with the code I've written, please tell me.
Well Time.time is the time in seconds since you started the application!
Sounds like you rather wanted to track the time since you instantiated the object so e.g. like
private float time;
void Update ()
{
time += Time.deltaTime * 0.5f;
WandPos.rotation = Quaternion.Lerp(new Quaternion(0,0,1,0), new Quaternion(0,0,0.866025448f,0.49999994f), time);
}
I'm making a small endless runner game about a fish swimming to school, and I want to include a particle system that replicates bubbles and indicates when it is speeding up. In order to make the fish swim through obstacles, he continually moves forward. However, when I try to move the particle generator forward through script, it doesn't work. Can someone tell me why?
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class motion : MonoBehaviour {
public float motionSpeed = 1f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(motionSpeed / 10, 0, 0);
motionSpeed += motionSpeed / 7200;
}
}
I recommend attaching your particle system onto a separate gameobject which is a child of your fish that is swimming.
I'm creating a teleporting for my PacMan game to get to the other side of the maze when triggered. I have the code then but when colliding nothing happens. I need that when i trigger the Left Portal i go to the right portal. I am thinking that the Pathfinding i have might be the problem. Thanks
Portal Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Portal : MonoBehaviour
{
public Transform warpTarget;
void onTriggerEnter2D(Collider2D other){
Debug.Log("An Object Collided");
other.gameObject.transform.position = warpTarget.position;
}
}
Maybe you should check for the methods's name. It must start with an uppercase char instead of an lowercase, so it should be something like this:
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("An Object Collided");
other.gameObject.transform.position = warpTarget.position;
}
I'm trying to make the system work so that when a game starts and for each wall, it will either be in the game or not, randomly. Somehow, when I run it, it either keeps or destroys all the walls. How do I fix this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallGen : MonoBehaviour{
public GameObject wallObject;
System.Random rnd = new System.Random();
// Use this for initialization
void Start () {
generate();
}
void generate()
{
int number = rnd.Next(1, 5);
if (number == 2)
{
OnDestroy();
}
}
private void OnDestroy(){
Destroy(wallObject);
}
// Update is called once per frame
void Update () {
}
}
Every time you do new System.Random() it is initialised using the clock. Now as the Start function is called at the same time, you get the same value for all the gameobjects. You should keep a single Random instance for all gameobjects and keep using Next on the same instance.
Or the easy solution
just Use Random.Range (1,5) instead of rnd.Next(1, 5);
As a side note, don't use the function name OnDestroy, as it is one of the MonoBehaviour functions in Unity and is called automatically when the attached gameobject is destroyed.