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 Xarlexus1 · Sep 16, 2013 at 06:47 AM · transformobjectcastobject-reference-errorconverting-var-type-to-other-var-type

What is wrong with my C# script?

Hello,

While translating a script from Unityscript to C# I received compiler errors when trying to start the game. The source of errors came from the "CharacterDamage" script. This is the content of the script:

 using UnityEngine;
 using System.Collections;
 
 public class CharacterDamage : MonoBehaviour
 {
     public Transform deadReplacement;
     public AudioClip dieSound;
     public float hitPoints = 100f;
 
     public void ApplyDamage(float damage)
     {
         if (hitPoints > 0f)
         {
             hitPoints -= damage;
             if (hitPoints <= 0f)
             {
                 Detonate();
             }
         }
     }
 
     public static void CopyTransformsRecurse(Transform src, Transform dst)
     {
         dst.position = src.position;
         dst.rotation = src.rotation;
         
         foreach (Transform child in transform) {
             Transform curSrc = src.Find(child.name);
             if (curSrc)
                 CopyTransformsRecurse(curSrc, child);
         }
     }
 
     public void Detonate()
     {
         Object.Destroy(gameObject);
         if (dieSound != null)
         {
             AudioSource.PlayClipAtPoint(dieSound, transform.position);
         }
         if (deadReplacement != null)
         {
             Transform dst = Object.Instantiate(deadReplacement, transform.position, transform.rotation);
             CopyTransformsRecurse(transform, dst);
         }
     }
 }

The following errors I got are:

CharacterDamage.cs(27,45): error CS0120: An object reference is required to access non-static member UnityEngine.Component.transform' > CharacterDamage.cs(43,23): error CS0266: Cannot implicitly convert type UnityEngine.Object' to `UnityEngine.Transform'. An explicit conversion exists (are you missing a cast?)

Please, what are the reasons why I am getting the errors? And how do I solve the problem?

Thank you,

Xarlexus1

Comment
Add comment · Show 3
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 Rasikh · Sep 16, 2013 at 06:51 AM 0
Share

Do you have added this script to a game object ?

avatar image getyour411 · Sep 16, 2013 at 07:00 AM 0
Share

43 remove Object.

avatar image Xarlexus1 · Sep 16, 2013 at 07:14 AM 0
Share

I can't add the script to a game object when it can't compile. I have tried removing Object at line 43, but I still am getting the same errors.

2 Replies

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

Answer by Bunny83 · Sep 16, 2013 at 07:37 AM

Line 27 should be:

     foreach (Transform child in src.transform) {

and Line 43 should be:

     Transform dst = (Transform)Object.Instantiate(deadReplacement, transform.position, transform.rotation);
Comment
Add comment · 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
1

Answer by blackcoper · Sep 16, 2013 at 07:54 AM

first : cannot access non-static member

         Transform allChildren = GameObject.Find("GameObject").transform;
         foreach (Transform child in allChildren) {
          Transform curSrc = src.Find(child.name);
          if (curSrc)
           CopyTransformsRecurse(curSrc, child);
        }

second : missing casting

  Transform dst = Object.Instantiate(deadReplacement, transform.position, transform.rotation) as Transform;
Comment
Add comment · 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

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

19 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

Related Questions

how to scale the transport? 1 Answer

How to move an object to your current positon? 0 Answers

Error: Assets/Scripts/AxeAttack.js(41,35): BCE0019: 'transform' is not a member of 'Object' 1 Answer

move object on x position to new specific x position 1 Answer

how to move object different positions in time 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