I am totally new to Unity and at the moment i am taking a course at Udemy. In course we have to create a game like flappy bird. We have created an object which has to move from right to left. Then we made a script which is as follow:
using UnityEngine;
public class Object : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(Vector3.left * (20 * Time.deltaTime));
}
}
The next thing we have to do connect this script to the object but than it goes wrong. It won't let me do that because there is an error in it. The error should be on line of the public class, but the teacher has the same code and it works for him. Is there something i am missing. This is very frustrating.
Thanks in advance.
According to
CS0589
The compiler found unexpected syntax.
I guess this have to do with this line.
public class Object : MonoBehaviour
Object is a default type in C#. Try chaning the name of the class.
Related
I'm new to Zenject and this is my first project using this asset. I'm having injection problems! Maybe someone knows what I am doing wrong or where the error might be. In the code below, _spawnArea is not initialized.
public class BootstrapIniter : MonoInstaller
{
[SerializeField] private Camera _mainCamera;
[Space(10)]
[SerializeField] private Spawner _spawner;
public override void InstallBindings()
{
BindMain();
BindBallHandle();
}
private void BindMain()
{
Container.Bind<Camera>().FromInstance(_mainCamera).AsSingle();
}
private void BindBallHandle()
{
Container.Bind<Spawner>().FromInstance(_spawner).AsSingle();
}
}
[RequireComponent(typeof(SpawnArea))]
public class Spawner : MonoBehaviour
{
private SpawnArea _spawnArea;
private void Awake()
{
_spawnArea = GetComponent<SpawnArea>();
}
[Inject]
public void Construct(Camera camera)
{
Rect cameraRect = camera.pixelRect;
_spawnArea.Init(cameraRect);
}
}
Thanks in advance for the answer or direction in which to look for a solution
I think that you did not inject your instance.
From the documentaiton "FromInstance - Adds a given instance to the container. Note that the given instance will not be injected in this case. If you also want your instance to be injected at startup, see QueueForInject" (QueueForInject will queue the given instance for injection once the initial object graph is constructed). Basically you need to inject your instance for the injected methods to execute.
On the other hand I dont see the point of binding a monobehaviour from instance, as you have to generate the instance bind it to the container and then inject it. You have binding methods that do this all at once for you, check the section "Construction Methods".
Check for example: FromComponentInNewPrefabResource - Instantiate the given prefab (found at the given resource path) as a new game object, inject any MonoBehaviour's on it, and then search the result for type ResultType in a similar way that GetComponentInChildren works (in that it will return the first matching value found).
Note that for the injection to take place succesfully you have to previously wire up the dependency in the container with the Container.Bind statement so that the container knows what needs to be injected and how.
I suggest to read carefully the documentation which is very good and follow the examples along.
For the past couple of days I've been trying to make a simple flappy bird game. At the moment, I'm trying to write some code that will make the score go up every time the player passes through two pipes. I'm getting an error though, and I'm not too sure how to fix it. This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Score : MonoBehaviour {
public static int score = 0;
private void Start() {
score = 0;
}
private void Update() {
GetComponent<UnityEngine.UI.Text>().text = score.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddScore : MonoBehaviour {
private void OnTriggerEnter2D (Collider2D collision) {
Score.score++; // This is the line that's giving me errors
}
}
The exact error I'm getting is - error CS0117: 'Score' does not contain a definition for 'score'. The reason I'm a bit confused is because on Vistual Studio Code it doesn't actually show any errors. The error only appears when I try to run the game.
Any help would be greatly appreciated.
error CS0117
In simple words, it occurs when you are trying to make a reference to a member that does not exist for the data type.
In order to fix it remove the references that you are trying to make which is not defined in the base class or look inside the definition of a type to check if the member exists and use that member as a reference.
For a better understanding you can have a look at this documentation : https://support.unity.com/hc/en-us/articles/205886966-What-is-CS0117-
Also you can change the datatype from int to float for more accurate score. From public static int score = 0; to public static float score = 0f;
I'm creating a script in unity extending Transform
using UnityEngine;
using System.Collections;
using UnityEditor;
public static class TransformExtension
{
//lots of functions
public static IEnumerator tester(this Transform test)
{
Debug.Log("hello");
yield return null;
}
public static void tester2(this Transform test)
{
Debug.Log("hello2");
}
}
when I invoke
transform.tester();
transform.tester2();
only "hello2" is logged.
when I tried
StartCoroutine(transform.tester());
i got the following errors:
"error CS0103: The name 'tester' does not exist in the current context"
"Transform' does not contain a definition for 'StartCoroutine' and no accessible extension method 'StartCoroutine' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)
when I tried
transform.StartCoroutine(transform.tester());
I got:
"error CS1061: 'Transform' does not contain a definition for 'StartCoroutine' and no accessible extension method 'StartCoroutine' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)"
You can not call a Coroutine like a method you rather have to start it via StartCoroutine(). When you call it like a normal method it will simply be ignored (as you already noticed).
You can't use transform.StartCoroutine() since Transform is of type Component and does not inherit from MonoBehaviour.
But StartCoroutine() can only be used on a MonoBehaviour.
So assuming you are already calling it from within a MonoBehaviour due to the usage of transform instead simply do
StartCoroutine(transform.tester());
which works completely fine for me as long as called from within a MonoBehaviour or alternatively
anyGameObject.GetComponent<MonoBehaviour>().StartCoroutine(transform.tester());
That other MonoBehaviour which will be running the Coroutine doesn't even have to be on the same object but you have to be sure there is any other MonoBehaviour script attached to anyGameObject.
You can not start coroutines like function calls Add a function which starts coroutine. Also since derHugo pointed out you need Monobehavior to achieve this you can access MonoBehavior over your transform like this:
public static IEnumerator Tester()
{
Debug.Log("hello");
yield return null;
}
public static void StartTester(this Transform test)
{
test.GetComponent<MonoBehaviour>().StartCoroutine(Tester());
}
public static void tester2(this Transform test)
{
Debug.Log("hello2");
}
Then do this:
transform.startTester();
I'm a beginner, so I'm sensing I'm making a simple mistake but I haven't been able to figure it out or find reference to a similar error on other forums.
My end goal is to create a graphic that changes colour depending on the time of day. Right now my issue is that I can not get a Date object to return anything for the life of me.
This is all I have put in a file called Main.as, that is called in one of the keyframes:
public class Main extends MovieClip {
var myDate1:Date = new Date();
trace(myDate1);
}
According to the API, if I don't define a specific date it should just take the current date from my system. But instead of doing the trace I keep getting "error 1120: Access of undefined property myDate1".
Why am I getting this error?
I should note I'm trying to make this for mobile so I've been testing the movie using AIR launcher.
Your script is wrong. You are not supposed to write code directly inside the class body. You need to define methods:
public class Main extends MovieClip
{
// Class constructor.
public function Main()
{
super();
// Output the current date.
trace(NOW);
}
// Static class property that always returns the current date.
static public function get NOW():Date
{
return new Date;
}
}
I have two spawn spots where a player will show up upon connection.
I need that, when one of the players connects on one of the spots, any other player will always spawn at the other spot.
Here's some visual in case it helps: https://goo.gl/Y0ohZC
Here is the code I'm using:
using UnityEngine;
using System.Collections;
public class SpawnSpot : MonoBehaviour {
public int teamId=0;
public GameObject[] Spots; //Drag the spots in here (In the editor)
bool[] OccupiedSpawnSpots;
//Using Photon Networking
void OnJoinedRoom()
{
//Request the recent OccupiedSpawnSpots List
PhotonView.RPC("RequestList", PhotonTargets.MasterClient, PhotonNetwork.player);
}
//In "RequestList" the MasterClient sends his List of the SpawnSpots
//by calling "ReceiveList"
[RPC]
void RequestList(PhotonPlayer player)
{
PhotonView.RPC("ReceiveList", PhotonTargets.All, player, OccupiedSpawnSpots);
}
[RPC]
void ReceiveList(PhotonPlayer Sender, bool[] ListOfMasterClient)
{
OccupiedSpawnSpots = ListOfMasterClient;
//Get the free one
if (OccupiedSpawnSpots[0] == false)
{
//Spawn player at 0
if (Sender == PhotonNetwork.player)
PhotonNetwork.Instantiate("PlayerController", Spots[0].transform.position);
OccupiedSpawnSpots[0] = true;
}
else
{
//Spawn player at 1
if (Sender == PhotonNetwork.player)
PhotonNetwork.Instantiate("PlayerController", Spots[1].transform.position);
OccupiedSpawnSpots[1] = true;
}
}
The errors given are:
Assets/Scripts/SpawnSpot.cs(14,28): error CS0120: An object reference
is required to access non-static member `PhotonView.RPC(string,
PhotonPlayer, params object[])'
Assets/Scripts/SpawnSpot.cs(22,28): error CS0120: An object reference
is required to access non-static member `PhotonView.RPC(string,
PhotonPlayer, params object[])'
Assets/Scripts/SpawnSpot.cs(36,47): error CS1501: No overload for
method Instantiate' takes3' arguments
Assets/Scripts/SpawnSpot.cs(43,47): error CS1501: No overload for
method Instantiate' takes3' arguments
Thanks in advance, IC
It appears that you are trying to call an instance function using a static reference. instead of doing PhotonView.RPC("RequestList", PhotonTargets.MasterClient, PhotonNetwork.player);
you need to create a reference to an PhatorView object, and then call the RPC function on it.
public PhotonView photonView;
void OnJoinedRoom()
{
if(photonView == null)
{
photonView = GetComponent<PhotonView>();
}
//Request the recent OccupiedSpawnSpots List
PhotonView.RPC("RequestList", PhotonTargets.MasterClient, PhotonNetwork.player);
}
you should have a PhotonView object on the same GameObject that this script is on, or assign a reference to a PhotonView in the editor.
That should fix your problems, but I think you should look into compiler errors and how to to fix them. In Unity3D if you double click the error in the console it will take you to the line that isn't compiling. It also tends to give you a pretty good hint as to why it isn't compiling.
your error was this
"Assets/Scripts/SpawnSpot.cs(14,28): error CS0120: An object reference is required to access non-static member `PhotonView.RPC(string, PhotonPlayer, params object[])'"
which means that you need an object in order to call this function.
The third and fourth errors are because you are calling Instantiate with only 2 arguments, but it takes at least 4. Include the rotation of the GameObject you're trying to instantiate, and a group number:
PhotonNetwork.Instantiate("PlayerController", Spots[0].transform.position, Quaternion.identity, 0);
Note that you may not want the identity quaternion, so you may need to change this. Also I'm not familiar with PhotonNetwork so 0 may not be an advisable group.