Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sdgd · Feb 28, 2013 at 04:05 AM · c#objectfunctionanything

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tarlius · Feb 28, 2013 at 04:33 AM 0
Share

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)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chronos-L · Feb 28, 2013 at 04:40 AM 0
Share

Forgot the Object.Equals(), should have thought of this one before the generic method.

avatar image sdgd · Feb 28, 2013 at 04:42 AM 0
Share

hey np man you did your best and I tried your way too

your input didn't go in vain anyway thanks :)

avatar image fafase · Feb 28, 2013 at 07:19 AM 1
Share

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.

avatar image
1

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.

C# Generic Method

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sdgd · Feb 28, 2013 at 04:31 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges