Adding Coroutine functions to Transform - unity3d

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();

Related

Argument exception when I try to set a variable with a custom set in unity

I tried to use the Generate() function only if a variable has changed without having to check it every frame. I used the following tutorial to achieve this. but for some reason, whenever i try to set the variable, I get this error:
ArgumentException: GetComponent requires that the requested component 'List`1' derives from MonoBehaviour or Component or is an interface.
the script:
public GameObject CEMM;
private int ListLength;
public static int ListLengthProperty
{
get
{
return JLSV.instance.ListLength;
}
set
{
JLSV.instance.ListLength = value;
JLSV.instance.Generate();
}
}
private void Awake()
{
instance = this;
}
I tried to set the value like this: JLScrollView.ListLengthProperty = JLScrollView.instance.CEMM.GetComponent<List<JLClass>>().Count;
The generic type parameter that you use when calling GetComponent must be a class that derives from Component (or an interface type). List is a plain old class object, which is why you are getting the exception from this:
GetComponent<List<JLClass>>()
I'm not really sure what value you are trying to assign to the property. If you are trying to get the number of components of a certain type on the GameObject you can use GetComponents.
JLScrollView.ListLengthProperty = JLScrollView.instance.GetComponents<JLClass>().Length;

Creating and using Roslyn Analyzers for Rider

So my situation is as follows:
I need to have a Code Analyzer that can detect if I'm using a Member (Method/Field/Property/Class) that is marked with the Attribute [EditorOnly] outside of a scope surrounded by a preprocessor that looks like this: #if UNITY_EDITOR ... #endif
To make a clear example:
I have the class with the editor-only method:
public class SampleClass {
#if UNITY_EDITOR
[EditorOnly]
public static void SampleMethod() {...}
#endif
}
and another class that is trying to use it at runtime:
public class BuildClass {
public void Start() {
SampleClass.SampleMethod();
}
}
A situation like this would fail to build as the call in the second script will not be able to find the method as it is excluded from the build.
So given what I said, the question is:
How do I create and use a Roslyn Analyzer that gives me a "Design-Time" error inside the IDE (JetBrains Rider) saying that I cannot use the method outside of the UNITY_EDITOR scope?
P.S. I don't want to use the [Conditional()] Attribute

Unity - error CS0117: 'Score' does not contain a definition for 'score'

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;

"DllAttribute could not be found": call a function in a .jslib file from unity

I tried to call a JavaScript Function from a .jslib file in Unity. I did it as described here
I have the .jslibe file in my assets/plugin folder. Now I tried to access it in the following code:
public class Prefab : MonoBehaviour
{
private string test = "test";
[DllImport("__Internal")]
private static extern void SendToJavscript(string test);
void Start()
{
SendToJavscript(this.test);
}
}
But it won´t compile and I get the error "The type or namespace name 'DllImportAttribute' could not be found (are you using directive or an assembly reference?)
Has anyone a idea what´s wrong here? I never got this error before..
At the top of your script add
using System.Runtime.InteropServices;

CS0589 unexpected syntax error in Unity3d

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.