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 Alarconte · Mar 05, 2014 at 01:35 AM · scripting problem

GetComponent Problem

I've been working all night on this. I don't know what more to do... The most simple of things (getComponent) doesn't work and I'm a bit confused.

So, this is just a debug start of the script, 'cause fails in the most simple of things:

(this object and invChar object are the same, was just for looking for the problem. Setting public the script components doesn't neither helps

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(InvEquipment))]
 public class AAL_CheckVisor : MonoBehaviour {
 
 
 
   
     public GameObject invChar;
     private InvEquipment allSlots;
     private FlashLight testScript;
     public int? itemID=null;
     public GameObject visor1;
 
 
 
     void Awake () {
 
         //DEBUG
         allSlots = this.GetComponent<InvEquipment> ();
 
         if (allSlots != null) {
                         Debug.Log ("There isn't InvEquipment in object");
                 } else {
                         Debug.Log ("there is InvEquipment in objeto");
                 }
         testScript = invChar.GetComponent<FlashLight> ();
 
         if (testScript != null) {
             Debug.Log ("No testScript i object");
         } else {
             Debug.Log ("testScript in object");
         }
 }

The Gameobject has the scripts attached. Everytime I run the game they report null. No need to say If I try to do something with them, I can cause the reference of the objects is empty.

So I'm lost. There is something clearly wrong? CAuse I don't have any idea why is unable to find anyobjects...

Thank you.

Comment
Add comment · Show 2
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 Jamora · Mar 05, 2014 at 01:39 AM 3
Share

You print "There isn't InvEq in object" if allSlots is NOT null. Try Debug.Log(allSlots); and see if that is null.

Are some of your scripts written in UnityScript and others in C#?

avatar image NoahConstable · Mar 05, 2014 at 09:12 AM 0
Share

This might be reiteration, but do you know the syntax for GetComponent? In case you don't it's:

 Your GameObject.GetComponent<Script On Your GameObject>(Arguments).Value You Need

For instance:

 timeLeftOnThisScript = TimerObject.GetComponent<TimerScript>().timeLeft;

Other than that obvious note, I'm looking deeper into your code to see if I can find any bugs.

1 Reply

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

Answer by Calum-McManus · Mar 05, 2014 at 03:13 PM

  if (testScript != null) {
          Debug.Log ("No testScript i object");
        } else {
          Debug.Log ("testScript in object");
        }

"!=" means not null, you are debugging "No testScript i object" when there is in fact a script on it.

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 Alarconte · Mar 05, 2014 at 08:17 PM 0
Share

Damm Yeah, I was changing the code so many times that thas was an error for my part. I have real problems with the real part of the script, I'll have put it all in here. I'll be back after rechecking all the script ;)

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to stop camera from spazzing out? 0 Answers

Enemy Spawn Script Spawning Infinite Enemies [Help] 2 Answers

Problem with shooting script 2 Answers

How to import the object from server to unity 2 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