- Home /
how to put in function any variables? like object?
using UnityEngine;
using System.Collections;
public struct UpdateYield {
public float time;
public float time1;
public object Compare2;
public bool UWaitForSecondsF (object Compare1, float YieldTime){
Debug.Log("in the function");
if (Compare2 != Compare1){
Debug.Log("Changing compare 2");
Compare2 = Compare1;
time = Time.time;
return false;
}
time1 = Time.time;
// if it's X seconds same return true
if (((time1 - time) > YieldTime) && Compare1 == Compare2){
return true;
}
return false;
}
}
// testing
public UpdateYield asdf;
void Update () {
bool bah = asdf.UWaitForSecondsF(5, 3);
if (bah){Debug.Log("yield time works");}
}
when I change both objects in to floats the code works
object Compare2; -- > float Compare2
how should I change my code that I could put anything inside and that it won't think all the time it's not same IF it hasn't been changed?
I know I could use coroutine but there are 1-3 ocasions I cannot do that in my code
and would like less thinking and more doing
I'm not really sure what your code is trying to achieve. Its possible there is a much simpler way.
Also be aware that if you start passing this struct around you are very likely to get unexpected results. (Structs are value types, and are copied when passed to a function. If you do something that will update it in the function, the effects won't be seen in the caller)
Answer by sdgd · Feb 28, 2013 at 04:28 AM
got it
using UnityEngine;
using System.Collections;
public struct UpdateYield {
private float time;
private float time1;
private object Compare2;
public bool UWaitForSecondsF (object Compare1, float YieldTime){
//Debug.Log("in the function");
if (! Compare1.Equals(Compare2)){
//Debug.Log("Changing compare 2");
Compare2 = Compare1;
time = Time.time;
return false;
}
time1 = Time.time;
// if it's X seconds same return true
if (((time1 - time) > YieldTime) && Compare1.Equals(Compare2)){
return true;
}
return false;
}
}
Forgot the Object.Equals()
, should have thought of this one before the generic method.
hey np man you did your best and I tried your way too
your input didn't go in vain anyway thanks :)
Note that the Object.Equal method might not be what you are expecting.
Depending on what you are passing, the comparison goes differently. If you pass a value type, you compare the actual value, example you pass integer 1 and 10 it returns they are different.
But if you pass a reference type, you actually compare the reference, so you are comparing if they point to the same object:
object a = new object();
object b = new object();
object c = a;
a.Equals(c) => true
a.Equals(b) => false
You may have to overwrite the Equals method to make it fit your need.
Answer by Chronos-L · Feb 28, 2013 at 04:16 AM
The answer to your question would be Generic Methods.
For comparing purposes, you can do something like this:
public bool Same<T>( T a, t b ) {
return ( a == b );
}
You will then use this as:
bool sameString = Same<string>("aaa", "abc");
bool sameInt = Same<int>(10, (5*2));
bool sameTransform = Same<Transform>( transform, gameObject.transform);
But you need to do a little bit more work to incorporate it into your script, I am just giving you a direction here.
no am I don't want it to search what type is it well anyway I managed to figure out how to make it work
and strange normal comparing doesn't work
anyway how would I demand that with function?
public bool (Same>T<) {
and give in what ever I want?
or you mean like this?
public bool UWaitForSecondsF >T<(>T< Compare1, float YieldTime){
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
is it possible to pass functions through functions? 2 Answers
Multiple Cars not working 1 Answer
CharacterController not found the GameObject 1 Answer
How can I call a function in all instances of a script 3 Answers