error CS0136

I need help to fix this: Assets/Scripts/GamePlay.cs(111,62): error CS0136: A local variable named pet' cannot be declared in this scope because it would give a different meaning to pet’, which is already used in a `parent’ scope to denote something else.

This is the script:

using UnityEngine;
using System.Collections;

public class GamePlay : MonoBehaviour {
   
    public GameObject[] petList;
    public int maxPetCount = 40;
   
    // An ArrayList for chained pet
    private ArrayList chainedList;
   
    // A place to keep all created pet to, easier management
    private GameObject petHolder;
   
    // Use this for initialization
    void Start () {
       
        // Create the place holder
        petHolder = new GameObject ("Holder");
       
        // Create the list
        chainedList = new ArrayList();
       
        // Move the pet creation to a function, so we can reuse it with
        // spawning different number
        Respawn (maxPetCount);
    }
   
    // Function to create pets
    void Respawn (int count) {
        for (int i=0; i<count; i++) {
            GameObject instance = Instantiate (petList[Random.Range(0, petList.Length)], new Vector3 (Random.Range(-2.6f, 2.6f), 7.0f, -1.0f), Quaternion.identity) as GameObject;
           
            // put the newly pet as the child of petHolder
            instance.transform.SetParent(petHolder.transform);
        }
    }
    // Update is called once per frame
    void Update () {
       
        if (Input.GetMouseButtonDown (0)) {
            // simulate touch began
           
            Vector3 p = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D hit = Physics2D.Raycast (p, Vector2.zero);
            if (hit.collider != null) {
                if (hit.collider.tag == "Pet") {
                    Pet pet = hit.collider.GetComponent<Pet> ();
                    // Add the pet to the chain
                    chainedList.Add (pet);
                    // Make it selected
                    pet.SetSelected (true);
                }
            }
        }
       
        if (Input.GetMouseButtonUp (0)) {
            // simulate touch ended
           
            // Only destroy pets in the chain with count > 2
            if (chainedList.Count > 2) {
                // Loop for every pet in the chain
                foreach (Pet pet in chainedList) {
                    // Destroy it
                    Destroy (pet.gameObject);
                }
                // Call respawn function to generate new pets
                Respawn (chainedList.Count);
            } else {
                // Chain count less than 3, just make the last pet unselected
                SetChainedLastPetSelected (false);
            }
           
            // Clear the chain list
            chainedList.Clear ();
        }
       
        if (Input.GetMouseButton (0) && chainedList.Count > 0) {
            // simulate touch moved
            Vector3 p = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D hit = Physics2D.Raycast (p, Vector2.zero);
           
            if (hit.collider != null) {
                if (hit.collider.tag == "Pet") {
                    Pet pet = hit.collider.GetComponent<Pet> ();
                   
                    // Check if this pet already inside the chain?
                    if (!chainedList.Contains(pet)) {
                        // Not inside the chain, is it valid to be chained?
                        if (GetLastPetInChain ().Validate (pet)) {
                            // Yes, make the last chained pet unselected
                            SetChainedLastPetSelected (false);
                            // Add to the chain
                            chainedList.Add (pet);
                            // Make it selected
                            pet.SetSelected (true);
                        }
                    } else {
                        // This pet is inside our chain, what is the index?
                        int index = chainedList.LastIndexOf (pet);
                        // Second last?
                        if (index == chainedList.Count - 2) {
                            // Backward to second last pet, unselect the last pet
                            SetChainedLastPetSelected (false);
                            // Remove the last one
                            chainedList.RemoveAt(chainedList.Count-1);
                            // Make the last pet selected
                            SetChainedLastPetSelected (true);

                        // Process at every frame no matter there are pets in the chain
                        foreach (Pet pet in petHolder.GetComponentsInChildren<Pet>()) {
                            pet.SetHighlight (false);
                        }
                           
                        // Highlight pet if there is a pet in the chain
                        if (chainedList.Count > 0) {
                            HighlightValidPet (GetLastPetInChain());
                            }
                        }
                    }
                }
            }
        }
    }
}

in line 85 you are definig a new variable with the name pet
Pet pet = hit.collider.GetComponent ();

in line 111 you are defining another new variable in the for each loop called pet
foreach (Pet pet in petHolder.GetComponentsInChildren()) {

you can t define another variable with the name pet while you already have a variable called pet

change lines 111 to 113 to:

                        foreach (Pet pet1 in petHolder.GetComponentsInChildren<Pet>()) {
                            pet1.SetHighlight (false);
                        }

The error has been solved, but 6 more errors have appeared … I am new to unity and I am following a tutorial for the version of unity 5 …
Can you send the script with the bugs fixed?

7063519--839170--d.PNG

I m confused, are you missing an entire class?
the errors you show simply tell you that you have no definition for those functions

you never defined what the function
SetChainedLastPetSelected(bool)
is even supposed to do…
you can not fix something that doesnt exist yet… it looks like you are simply missing code

are you sure you have all the scripts of that tutorial already added to your unity project?

1 Like

Remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all.

The important parts of an error message are:

  • the description of the error itself
  • the file it occurred in
  • the line number and character position.

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly.

How to understand compiler and other errors and even fix them yourself:

https://discussions.unity.com/t/824586/8

can anyone help me in solving this error CS0136: A local or parameter named ‘targetRotation’ cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationAndMovementController : MonoBehaviour
{
PlayerInput playerInput;
CharacterController characterController;
Animator animator;
Vector2 currentMovementInput;
Vector3 currentMovement;
bool isMovementPressed;
float rotationFactorPerFrame = 1.0f;
void Awake()
{
playerInput = new PlayerInput();
characterController = GetComponent();
animator = GetComponent();
playerInput.CharacterControls.Move.started += context => {
currentMovementInput = context.ReadValue();
currentMovement.x = currentMovementInput.x;
currentMovement.z = currentMovementInput.y;
isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
};
playerInput.CharacterControls.Move.canceled += context => {
currentMovementInput = context.ReadValue();
currentMovement.x = currentMovementInput.x;
currentMovement.z = currentMovementInput.y;
isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
};
playerInput.CharacterControls.Move.performed += context => {
currentMovementInput = context.ReadValue();
currentMovement.x = currentMovementInput.x;
currentMovement.z = currentMovementInput.y;
isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
};
}
void handelRotation()
{
Vector3 positionToLookAt;
positionToLookAt.x = currentMovement.x;
positionToLookAt.y = 0.0f;
positionToLookAt.z = currentMovement.z;
Quaternion currentRotation = transform.rotation;
Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
if (isMovementPressed) {
Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame);
}
}
void handelAnimation()
{
bool isWalking = animator.GetBool(“IsWalking”);
bool isRunning = animator.GetBool(“IsRunning”);
if (isMovementPressed && !isWalking) {
animator.SetBool(“IsWalking”, true);
}
else if (!isMovementPressed && isWalking) {
animator.SetBool(“IsWalking”, false);
}
}
// Update is called once per frame
void Update()
{
handelRotation();
handelAnimation();
characterController.Move(currentMovement * Time.deltaTime);
}
void OnEnable()
{
playerInput.CharacterControls.Enable();
}
void OnDisable()
{
playerInput.CharacterControls.Disable();
}
}

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

It seems you have failed to read immediately above your post how to fix errors above, so I will post it again for you.

Remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

Always start with the FIRST error in the list, as sometimes that error causes or compounds some or all of the subsequent errors.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

How to understand compiler and other errors and even fix them yourself:

https://discussions.unity.com/t/824586/8