• 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 Thet Naing Swe · Aug 24, 2010 at 02:07 PM · gameobjectaccess

C# Gameobject Problem

What's wrong with the access to GameObject when using C# although I haven't tested using Javascript?

I've got an enemy character which will follow the player's path. the code is written below.

using System; using System.Collections.Generic; using System.Text; using UnityEngine;

public class EnemyCharacter : MonoBehaviour { public GameObject player; public Transform[] path;

 public void Start()
 {
     //Get the Player Tag
 }

 public void FixedUpdate()
 {
     if (!player)
         return;

     print("Player Exist");
 }

}

I attached this script to my enemy character and run the Unity and I get 1700fps ... then I set the value "player" in the editor and then the framerate becomes less than 300 fps.. What's wrong with that??? Is that the Bug for Unity 3.0? I am using Unity3.0 beta 5...

Comment
Add comment
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

2 Replies

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

Answer by Daniel-Brauer · Aug 24, 2010 at 02:42 PM

It's hard to reasonably compare frame rates above 500 with anything. If your frame rate is above 500, you're probably not doing a lot.

Compared to just checking a reference for null, printing to the log is a relatively slow operation. That said, most operations are way slower than checking for null. Don't worry about logging: I doubt you'll see a noticeable effect if your game is running at a normal frame rate to begin with.

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 Wolfram · Aug 24, 2010 at 03:13 PM 0
Share

I agree, the problem for your framerate drop is most definitely the print(), since it is only called if "player" is set. Remove the line and see if that changes the fps rate.

avatar image Daniel-Brauer · Aug 24, 2010 at 07:04 PM 0
Share

I think Thet realized that print() was causing the drop. $$anonymous$$y point is just that it's not an actual problem, because going from an obviously-not-doing-anything 1700fps to a still-higher-than-you'll-ever-need 300fps is nothing to worry about. There are a lot of innocuous functions that would cause a similar drop.

avatar image Thet Naing Swe · Aug 26, 2010 at 05:37 AM 0
Share

I agree that print() is making the framerate Drop... The only thing I am worried is that using $$anonymous$$ove Function in Character Controller also make the famedrop... It can give the reasonable framerate on PC with about 20 Enemy Characters with Character Controller attacked to it and moving Randomly... How about making them works in iPhone Version. It's gonna be the problem.. Any alternative ways to Character Controller ? I've tried usign Rigidbody and got the same problems..

avatar image
0

Answer by qJake · Aug 24, 2010 at 02:37 PM

Because you're getting errors in the console, which you also failed to mention in your post. You can't do "!player" because "player" is not a boolean type. You need to use "player == null" instead.

Comment
Add comment · Show 7 · 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 Daniel-Brauer · Aug 24, 2010 at 02:43 PM 0
Share

Actually, operator bool is overloaded for all UnityEngine.Objects, including GameObject. It's a neat little trick that shortens your null checks.

http://unity3d.com/support/documentation/ScriptReference/Object-operator_bool.html

avatar image Thet Naing Swe · Aug 24, 2010 at 02:45 PM 0
Share

even I change the code with ur suggestion, it stills get an error... :(

avatar image Daniel-Brauer · Aug 24, 2010 at 02:54 PM 0
Share

You're "still" getting an error? You should really mention errors if you're getting them, because they're far more important than frame rate. What error were you getting before, and what error are you getting now?

avatar image Wolfram · Aug 24, 2010 at 03:05 PM 0
Share

@$$anonymous$$: you can do if(myGameObject), but you can't do if(!myGameObject) [at least that's how it is in Unity's C#]

avatar image Wolfram · Aug 24, 2010 at 03:09 PM 0
Share

...uh...no...wait...it does work? Strange, I distinctly remember always running into compiler errors when using "!" for objects (don't remember what kind of objects, though).

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Is it OK to use public accessers for unique gameobjects (C#) ? 3 Answers

How can I access other scripts and their functions? 3 Answers

how to call animations from a different game object 2 Answers

Whats the best way to access other Game Objects? 2 Answers

Access objects enum type 1 Answer


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